Loading Images into an array of PImages, question with sprites[si] = loadImage(SpriteNames[si]);
in
Programming Questions
•
2 years ago
I'm modifying someone's script here to show many different images instead of one image of a ball. The following code was working fine until I added in the three lines 21-23 and commented out the lines below; I may want to use more or different images and want to be able to select random images so I would preferable put the names of the images to load in an array of file names, and then in setup do a for loop on each of the names through loadImage(StringArray[index]) ..
I think lines 21-23 broke my code. It throws a NullPointerException. Do I need to set up the size of sprites somewhere before adding to it?
I think lines 21-23 broke my code. It throws a NullPointerException. Do I need to set up the size of sprites somewhere before adding to it?
- PGraphics[] spriteFrames = new PGraphics[6];
- //PImage sprite;
- //PImage sprite2;
- //PImage sprite3;
- //PImage sprite4;
- //PImage sprite5;
- //PImage sprite6;
- //PImage sprite7;
- String[] SpriteNames = {"sm8_rockstar.jpg","sm12_blonde.jpg","sm2_min.jpg","sm10_ozzy.jpg","sm3_pol.jpg","sm13_.jpg","sm7_guitar.jpg"};
- PImage[] sprites;
- float x, y, xs, ys;
- float xang = 0.0;
- float yang = 0.0;
- int surf = 0;
- void setup() {
- size(640, 360);
- noSmooth();
- background(0);
- frameRate(30);
- for (int si = 0; si < SpriteNames.length;si++) {
- sprites[si] = loadImage(SpriteNames[si]);
- }
- // sprite=loadImage("sm8_rockstar.jpg");
- // sprite2=loadImage("sm12_blonde.jpg");
- // sprite3=loadImage("sm2_min.jpg");
- // sprite4=loadImage("sm10_ozzy.jpg");
- // sprite5=loadImage("sm3_pol.jpg");
- // sprite6=loadImage("sm13_.jpg");
- // sprite7=loadImage("sm7_guitar.jpg");
- // Create blank surfaces to draw on
- for (int i = 0; i < spriteFrames.length; i++) {
- spriteFrames[i] = createGraphics(width, height, JAVA2D);
- }
- }
- void draw()
- {
- background(0);
- // Get X, Y positions
- x = (width/2)*sin((radians(xang))*0.95);
- //xs = x - sprite.width;
- y = (height/2)*cos((radians(yang))*0.97);
- //ys = y - sprite.height;
- // Inc the angle of the sine
- xang += 1.17;
- yang += 1.39;
- // Blit our 'bob' on the 'active' surface
- spriteFrames[surf].beginDraw();
- for (int si = 0;si<=sprites.length;si++) {
- spriteFrames[surf].image(sprites[si], x+(width/2)-32, y+(height/2)-32);
- x = x + 10;
- y = y +10;
- }
- // spriteFrames[surf].image(sprite, x+(width/2)-32, y+(height/2)-32);
- // spriteFrames[surf].image(sprite2, xs+(width/2)-32, ys+(height/2)-32);
- //
- // spriteFrames[surf].image(sprite3, xs*2+(width/2)-32, ys*2+(height/2)-32);
- // spriteFrames[surf].image(sprite4, xs*3+(width/2)-32, ys*3+(height/2)-32);
- // spriteFrames[surf].image(sprite5, xs*2-(width/2)-32, ys*2-(height/2)-32);
- // spriteFrames[surf].image(sprite6, xs*3-(width/2)-32, ys*3-(height/2)-32);
- // spriteFrames[surf].image(sprite7, xs+(width/2)-32, ys-(height/2)-32);
- spriteFrames[surf].endDraw();
- // Blit the active surface to the screen
- image(spriteFrames[surf], 0, 0, width, height);
- // Inc the active surface number
- surf = (surf+1) % spriteFrames.length;
- // Display the results
- //image(spriteEffect, 0, 0, width, height);
- }
1