This site requires JavaScript, please enable it in your browser!
Greenfoot back
Omniscience
Omniscience wrote ...

2012/2/2

Manipulating Arrays [-_-]

Omniscience Omniscience

2012/2/2

#
Having some trouble with manipulating arrays to my advantage here. I'm creating a game similar to CRAZY TAXI (dunno if you've heard of it), a car game where you avoid oncoming cars on an ever extending road. I have decided to add some images (that of the Actor named "Question") that will randomly appear and travel down the road. I am currently storing the three locations in which they originate on the x-axis with an array, stored within the Actor Class: public static int QuestionXPosition = {232,240,249}; The World class then executes this array. Greenfoot.getRandomNumber(Probability); if (RanNum == 0) { Question question = new Question(); addObject(question, Question.QuestionXPosition, Question.ObjectYPosition); } if (RanNum == 1) { Question question = new Question(); addObject(question, Question.QuestionXPosition, Question.ObjectYPosition); } if (RanNum == 2) { Question question = new Question(); addObject(question, Question.QuestionXPosition, Question.ObjectYPosition); } I now want the images' linear direction of movement down the road to change according to where they originate. For example, the leftmost image should travel downwards diagonally to the left. The central image should travel straight down, whilst the rightmost image should travel downwards diagonally to the right. I have a variable named XLocate which determines how much to the left or right the image travels as it descends down the road. The only trouble is adjusting this variable depending on which of the three locations the image takes (defined within the array). I would really appreciate any ideas as to how I should adjust my code so that the variable XLocate alters according to what position is retrieved from the array, and also which class I should store the solution in... i.e. in the World or the Actor. Thankyou!
Builderboy2005 Builderboy2005

2012/2/2

#
Instead of using a whole bunch of If statements, it is easier to do something like this:
ranNum = Greenfoot.getRandomNumber(Probability); 
Question question = new Question();
addObject(question, Question.QuestionXPosition[ranNum], Question.ObjectYPosition);
Where ranNum is an integer that has been initialized by the world. You can then use this ranNum to get the correct XLocate later on by using it as the argument to the array. For instance your code could look something like this:
int[] XLocateArray = {0,1,2};
XLocate = XLocateArray[ranNum];
As for whether or not to put this code in an Actor or the World is really up to you and what you think of the behavior of the Question actor. Do you want to be able to create more than 1 actor at a time? If so probably most of your code would go in the actor.
Omniscience Omniscience

2012/2/2

#
@Builderboy2005 thankyou for the response, I am very grateful! If I may, I would like to ask a dumb question... what effect does the piece of code that says have? I'm really new to forming arrays, so sorry for the dumb question! :)
Builderboy2005 Builderboy2005

2012/2/2

#
There are no dumb questions ^^ whenever you see the code 'array', all that it means is that you are accessing the #th element of that array. When I say , what it does is pick out the array element that corresponding to the number stored in ranNum. If ranNum is 0 it will pick out the first element, if ranNum is 1 it will pick out the second element, and so on. You might want to do some reading up on arrays yourself, as there is definitely a lot of documentation on the web! A lot more than I can pour into one post anyway.
Duta Duta

2012/2/3

#
Builderboy2005 wrote...
Instead of using a whole bunch of If statements, it is easier to do something like this:
ranNum = Greenfoot.getRandomNumber(Probability); 
Question question = new Question();
addObject(question, Question.QuestionXPosition[ranNum], Question.ObjectYPosition);
Where ranNum is an integer that has been initialized by the world. You can then use this ranNum to get the correct XLocate later on by using it as the argument to the array. For instance your code could look something like this:
int[] XLocateArray = {0,1,2};
XLocate = XLocateArray[ranNum];
As for whether or not to put this code in an Actor or the World is really up to you and what you think of the behavior of the Question actor. Do you want to be able to create more than 1 actor at a time? If so probably most of your code would go in the actor.
Everything I'm about to say is assuming that the int Probability higher than 3. If this isn't the case, ignore all of the below: (1) An OutOfBounds exception may be thrown (because ranNum may be > 2 and therefore out of the bounds of the array QuestionXPosition. (2) This is doing a slightly different job to the original code: this will always create a Question and add it to the world, however the original code has a 3 in Probability chance of creating a Question (and adding it to the world).
Builderboy2005 Builderboy2005

2012/2/3

#
Yes, I was operating under the assumptions that Probability was 3, although a simple If() check would fix any differences
Omniscience Omniscience

2012/2/3

#
I see what you are saying... Probability is quite large, i.e. bigger than 100, because the chance of this "Question" appearing must be relatively small (otherwise the game would be too hard!). Yes, a simple If statement would manage RanNum in this instance. However, I need the variable Probability, and whatever value it produces, to always be valid, thus creating a situation where there is a 3 in Probability chance of the "Question" occurring. The QuestionXPosition must remain as the values shown: (read the original script), the only change required its an incorporation of how far to the left or right it travels, using XLocate! :)
You need to login to post a reply.