Slick Forums

Discuss the Slick 2D Library
It is currently Wed Jun 19, 2013 1:19 pm

All times are UTC




Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: Arrays and sounds
PostPosted: Sat Aug 18, 2012 1:44 pm 
Offline

Joined: Thu Aug 09, 2012 11:07 pm
Posts: 4
All of my objects, like coins, are stored in an array, but I can't figure out why this isn't working.
If I do this:
Code:
GameObject[] objectsArray = {new ObjCoin(100,100), new ObjCoin(100,200), new ObjCoin(100,300), new ObjCoin(100,400), new ObjCoin(100,500), new ObjCoin(100,600)};
objects = objectsArray;
for(int i = 0; i < 6; i++){
   objectsArray[i] = new ObjBlock(50+30*i, 100);
}

This works fine. I get a straight line of blocks with this.

But with this code:
Code:
GameObject[] objectsArray = new GameObject[1000];
objects = objectsArray;
for(int i = 0; i < 6; i++){
   objectsArray[i] = new ObjBlock(50+30*i, 100);
}

I get a null pointer exception when trying to access the objects in the array in my render and update function. Any ideas why?
I'd also like to know how to handle sounds.


Top
 Profile  
 
 Post subject: Re: Arrays and sounds
PostPosted: Sat Aug 18, 2012 3:46 pm 
Offline
Slick Zombie

Joined: Sat Jan 27, 2007 7:10 pm
Posts: 1477
Elements in an object array are initially null. You've got a big array -- 1000 elements -- and I bet you loop through each and forget to check whether it's null or not, which is causing the NPE.

Instead you should use an ArrayList<GameObject> which will be much easier to work with. There are some code examples in the Slick trunk that might give you a better idea how to handle your entities: (Virium, Puzzle, Platformer, etc)
https://bitbucket.org/kevglass/slick/src/tip/trunk/

For sounds, you just load them and play them. Check the sound or music tests. What part do you not understand?


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group