We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi,
I'm sort of new to Processing. I've recreated flappyBird using the android mode for processing.
It runs when I test the device. However, when the bird falls down, processing returns this error:
FATAL EXCEPTION: Animation Thread
Process: processing.test.flappybird, PID: 20383
java.lang.NullPointerException
at android.graphics.Bitmap.checkPixelsAccess(Bitmap.java:1383)
at android.graphics.Bitmap.setPixels(Bitmap.java:1445)
at processing.core.PGraphicsAndroid2D.imageImpl(Unknown Source)
at processing.core.PGraphics.image(Unknown Source)
at processing.core.PApplet.image(Unknown Source)
at processing.test.flappybird.flappyBird.printNum(flappyBird.java:386)
at processing.test.flappybird.flappyBird.draw(flappyBird.java:234)
at processing.core.PApplet.handleDraw(Unknown Source)
at processing.core.PGraphicsAndroid2D.requestDraw(Unknown Source)
at processing.core.PApplet.run(Unknown Source)
at java.lang.Thread.run(Thread.java:841)
I've also attached the flappyBird.pde for people to look at:
import ketai.sensors.*;
//import ddf.minim.*;
import apwidgets.*;
import android.os.Environment;
APMediaPlayer wingSound, hitSound, scoreSound, dieSound;
//Minim minim;
Table tsv;
ArrayList<Stump> stumps;
float stumpMIN=0,stumpMAX=0,stumpDiffY=150,stumpDiffX=250;
int stumpCount=0;
int birdX,score=0,nextStump=0,i,TopScore;
PImage[] imgNumBig,imgNumSmall;
Boolean hit=false,hitSnd=false,stumpHit=false,gameOver=false;
PVector birdPosition,velocity,gravity,up;
PImage[] bird;
PImage background,base,stump,stumpi;
int brd=0,deg=0,baseInc=0;
float BY,BGY;
int scene=0;
PImage imgTitle,imgGetReady;
PImage imageGameOver,imageScoreCard,imageClick,goldScoreCard;
int gameOverPosY,scoreCardPosY;
char ch;
void setup(){
size(1000,700);
//topScoreFileLoader();
wingSound = new APMediaPlayer(this); //create new APMediaPlayer
wingSound.setMediaFile("wing.mp3"); //set the file (files are in data folder)
wingSound.start(); //start play back
wingSound.setLooping(true); //restart playback end reached
wingSound.setVolume(1.0, 1.0); //Set left and right volumes. Range is from 0.0 to 1.0
hitSound = new APMediaPlayer(this); //create new APMediaPlayer
hitSound.setMediaFile("hit.mp3"); //set the file (files are in data folder)
hitSound.start(); //start play back
hitSound.setLooping(true); //restart playback end reached
hitSound.setVolume(1.0, 1.0); //Set left and right volumes. Range is from 0.0 to 1.0
scoreSound = new APMediaPlayer(this); //create new APMediaPlayer
scoreSound.setMediaFile("point.mp3"); //set the file (files are in data folder)
scoreSound.start(); //start play back
scoreSound.setLooping(true); //restart playback end reached
scoreSound.setVolume(1.0, 1.0); //Set left and right volumes. Range is from 0.0 to 1.0
dieSound = new APMediaPlayer(this); //create new APMediaPlayer
dieSound.setMediaFile("die.mp3"); //set the file (files are in data folder)
dieSound.start(); //start play back
dieSound.setLooping(true); //restart playback end reached
dieSound.setVolume(1.0, 1.0); //Set left and right volumes. Range is from 0.0 to 1.0
/**
wingSound = minim.loadSample("wing.mp3",512);
hitSound = minim.loadSample("hit.mp3",512);
scoreSound = minim.loadSample("point.mp3",512);
dieSound = minim.loadSample("die.mp3",512);
*/
imgNumBig = new PImage[10];
imgNumSmall = new PImage[10];
for(i=0;i<10;i++){
imgNumBig[i]=loadImage(i+".png");
imgNumSmall[i]=loadImage(i+".png");
imgNumSmall[i].resize(13,18);
}
tsv = new Table();
// make the header columns for the table
tsv.addColumn("High Score");
bird = new PImage[3];
bird[0]=loadImage("bird1.png");
bird[1]=loadImage("bird2.png");
bird[2]=loadImage("bird3.png");
base=loadImage("base.png");
stump=loadImage("stump.png");
stumpi=loadImage("stumpi.png");
stumps = new ArrayList<Stump>();
stumpCount = (int) (width/(stumpDiffX+stump.width));
stumpCount++;
stumps.add(new Stump(random(stumpMIN,stumpMAX),0));
for(int k=1;k<=stumpCount;k++){
stumps.add(new Stump(random(stumpMIN,stumpMAX),stumpDiffX*k));
}
imgTitle=loadImage("title.png");
imgGetReady=loadImage("getReady.png");
imageGameOver=loadImage("gameOver.png");
imageScoreCard=loadImage("scoreCard.png");
imageClick=loadImage("click.png");
goldScoreCard=loadImage("goldScoreCard.png");
initORreset();
}
void draw(){
//if(ch=='L'){ lc=1;}
//if(ch=='R'){ lc=0;}
if (keyPressed) {
if(key =='R'|| key =='L')//if(lc==1)
{
switch(scene)
{
case 0: //title screen
scene=1;
break;
case 1: //get ready
scene=2;
break;
case 2: //game
if(!stumpHit)
{
velocity.add(up);
wingSound.start();
}
break;
case 3: //game over
initORreset();
scene=1;
break;
}
}
}
TableRow row = tsv.addRow();
row.setInt("High Score", TopScore);
drawBackground();
textSize(35);
fill(255,100);
textAlign(CENTER);
switch(scene)
{
case 0:
case 1:
image(base,baseInc,BY);
if(scene==0)
{
image(imgTitle,width/2 - imgTitle.width/2,height/4);
image(bird[brd/10],width/2 - bird[0].width/2,height/2 + sin(radians(deg)*10)*5 -50);
}
else
{
image(imgGetReady,width/2 - imgGetReady.width/2,height/4);
image(bird[brd/10],width/4,height/2 + sin(radians(deg)*10)*5 -50);
image(imageClick,width/2 - imageClick.width/2,height/3 +50);
}
brd+=1;
deg+=1;
if(brd>20) brd=0;
if(deg>360) deg=0;
break;
case 2:
for(int k=0;k<stumps.size();k++){
Stump st = stumps.get(k);
st.checkHit();
if(!stumpHit && !hit)
{
st.checkPassed();
checkScored();
st.update();
}
else
{
gameOver=true;
}
st.display();
}
image(base,baseInc,BY);
flyingBird();
/**
stroke(0);
line(0,stumpMIN,width,stumpMIN);
stroke(#ffffff);
line(0,stumpMAX,width,stumpMAX);
*/
if(hitSnd){hitSound.start(); fill(255); for(int z=0;z<100;z++) rect(0,0,width,height); hitSnd=false; if(stumpHit) dieSound.start();}
printNum(score,width/2,height/6,'b');
break;
case 3:
for(int k=0;k<stumps.size();k++){
Stump st1 = stumps.get(k);
st1.display();
}
image(base,baseInc,BY);
translate(birdPosition.x,birdPosition.y);
rotate(radians(90));
image(bird[0],-bird[0].width/2,-bird[0].height/2);
resetMatrix();
if(gameOverPosY<height/6) gameOverPosY+=5;
if(scoreCardPosY>height/6 +100) scoreCardPosY-=10;
image(imageGameOver,width/2 - imageGameOver.width/2,gameOverPosY);
if(score>TopScore)
image(goldScoreCard,width/2 - goldScoreCard.width/2,scoreCardPosY);
else
image(imageScoreCard,width/2 - imageScoreCard.width/2,scoreCardPosY);
//print Topscore
printNum(score>TopScore?score:TopScore,width/2 + 90,scoreCardPosY+75,'s');
//print score
printNum(score,width/2 + 90,scoreCardPosY+35,'s');
break;
}
}
void mousePressed(){
switch(scene)
{
case 0: //title screen
scene=1;
break;
case 1: //get ready
scene=2;
break;
case 2: //game
if(!stumpHit)
{
velocity.add(up);
wingSound.start();
}
break;
case 3: //game over
initORreset();
scene=1;
break;
}
}
void initORreset()
{
if(score>TopScore)
{
TopScore=score;
saveFile("high_score_data", tsv);
//topScoreFileUpdator();
}
hit=false;hitSnd=false;stumpHit=false;gameOver=false;
score=0;nextStump=0;
birdX=width/4;
birdPosition = new PVector(birdX,height/2 -50);
velocity = new PVector(0,0);
gravity = new PVector(0,0.4);
up = new PVector(0,-8);
if((int)random(2)<1){
background=loadImage("dayCity.png");
}
else{
background=loadImage("nightCity.png");
}
BGY=-1*(background.height-height+base.height);
BY=height-base.height;
stumpMIN = BY/6;
stumpMAX = BY-stumpDiffY-stumpMIN;
gameOverPosY=imageGameOver.height*-1;
scoreCardPosY=height;
// print(stumps.size());
for(int k=0;k<stumps.size();k++){
Stump st2 = stumps.get(k);
st2.posY=random(stumpMIN,stumpMAX);
st2.posX=stumpDiffX*k+width;
}
}
void saveFile(String name, Table table){
String directory;
try{
directory = new String(Environment
.getExternalStorageDirectory()
.getAbsolutePath());
table.save(new File(directory+"/"+name+".tsv"), "tsv");
println("File write successful");
}catch(IOException iox){
println("Failed to write file: "+iox.getMessage());
}
}
Help would be greatly appreciated.
Answers
where's printNum() in the code you posted?
koogs. Sorry for not posting it. It is a separate .pde file:
Code:
the error suggests it's this that's causing the problem but it looks ok, at a glance. check all the images (0.png etc) are there.
add
into the loading loop, make sure they load. (after line 72 in the first post)
(obviously we can't sensibly run it without the images)
koogs, ty I hope this will work
It loaded the images:
It put it in the console:
But the error still shows:
ok, instead of printing those out try drawing the images to the screen.
something like
in that loop
and
add a println for n, xPos, Ypos, bigOrSmall to the top of printNum and see what the last line says when it crashes. do this a few times, see if there's a pattern to the values.
koogs, could it be because imgNumSmall[i].resize(13,18); I looked up the resize() method and apparently this method doesn't work in the android mode of processing. It works in java mode however.
could be.
try adding a set of images of the right size and load them.
yes. I've figured it out. I have put a loadPixels() after the method. Now it seems to work. YAY :D
Now I have a different problem. I don't know if you could help, but I created a table (.tsv file) that saves the current highScore. However, in the console it gives me this error:
If you're saving to the external storage, you'll need to add the sketch permission
WRITE_EXTERNAL_STORAGE
from the Android > Sketch Permissions menu.