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

2011/10/27

Need help making a movement array

UMike UMike

2011/10/27

#
Hello! I need help making an array for the movement of a character. Originally I had a balloon move by keystroke movements and the getX()/gety()+direction methods,now I would like to make an integer string array based on the getx()/getY() method but i'm confused as to how to map it. Does anyone know how I can do this?
danpost danpost

2011/10/27

#
Would the array be pre-set to guide the balloon on its path, or filled in as the balloon goes on its course? Also, what information would you like for the array to carry (x and y coordinates? speed and direction? or something else?). I understand what you had, originally; I am just not sure precisely what you are trying to do with the array.
UMike UMike

2011/10/27

#
Yes, I want it to be a filled in array of x and y coordinates so that the balloon will be able to move around the screen when the player presses one of the arrow keys.
danpost danpost

2011/10/27

#
So the player presses an arrow key, but the balloon goes to the next location in the array?
UMike UMike

2011/10/27

#
Not really, I still the balloon to move freely but I just change something like this: private void balloonFloatHorizontal (int distance, int direction, String img1, String img2) { for (int i = 0; i < distance; i++) { setImage(img1); wait(2); setLocation(getX() + direction, getY()); setImage(img1); wait(2); setLocation(getX() + direction, getY()); } into a string array like this: this.balloonImg = new GreenfootImage{new GreenfootImage ("balloon3.png"),new GreenfootImage ("balloon4.png"), new GreenfootImage("balloon2.png"), new GreenfootImage ("balloon1.png") }; or private String directionKey= ("left","right", "up", "down");
danpost danpost

2011/10/27

#
I am sorry, but I think I will leave this one for someone else.
davmac davmac

2011/10/27

#
UMike, I'm having trouble understanding what it is you want exactly. It seems like you understand the syntax for declaring an array; what's not clear is how you want to use the array exactly. If you want to use an array index rather than specifying the two strings each time you could write:
private void balloonFloatHorizontal (int distance, int direction, int img1, int img2)
{
    for (int i = 0; i < distance; i++)
    { 
       setImage(balloonImg[img1]);
       wait(2);
       setLocation(getX() + direction, getY());
       setImage(balloonImg[img1]);
       wait(2);
       setLocation(getX() + direction, getY());
    }
}
... but I'm not really sure that's what you meant.
UMike UMike

2011/10/28

#
Actually, I got help from my professor today and she suggested that I adding this to my code and it worked: Constructor: this.xCoordinate= new int; this.yCoordinate= new int ; method array: { int currentX = getX( ); this.xCoordinate = currentX + Greenfoot.getRandomNumber (550-currentX); this.xCoordinate = currentX + Greenfoot.getRandomNumber (550-currentX); this.xCoordinate = currentX + Greenfoot.getRandomNumber (550-currentX); this.xCoordinate = currentX + Greenfoot.getRandomNumber (550-currentX); this.xCoordinate = currentX + Greenfoot.getRandomNumber (550-currentX); int currentY = getY( ); this.yCoordinate = currentY + Greenfoot.getRandomNumber (550-currentY); this.yCoordinate = currentY + Greenfoot.getRandomNumber (550-currentY); this.yCoordinate = currentY + Greenfoot.getRandomNumber (550-currentY); this.yCoordinate = currentY + Greenfoot.getRandomNumber (550-currentY); this.yCoordinate = currentY + Greenfoot.getRandomNumber (550-currentY); } Thank you for your help though! =D
You need to login to post a reply.