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

2013/12/30

New idea, holding an object via space bar

Moynzy Moynzy

2013/12/30

#
Dear green foot users, I have made a game where the baby can kick the ball in 8 directions. Now I'm trying some code but it's not going well. When the baby is touching the ball and the space bar is being held I will like the baby to hold the ball. //if(there is detection with baby, -10, 0) && and space bar is pressed) { // then set the balls x and y position to the babys position}
danpost danpost

2013/12/30

#
Better would be to put this code in the class of the baby: //if(there is detection with ball, 10, 0) && and space bar is pressed) { // then set the balls x and y position to this babys position } One way to determine the best place for the code is to ask yourself: "is it the ball that latches onto the baby or is it the baby that latches onto (grabs) the ball?" What object is really doing the action here? the ball or the baby? Usually, the coding will be easiest when put in the proper place (although, in this simple case it may not matter much). The steps involved will be: (1) get reference to ball (or null, if not detected) (2) 'if' condition: if referenced ball is not null and spacebar is pressed (3) 'if' block: set location of referenced ball to that of the 'this' baby
Moynzy Moynzy

2013/12/31

#
public void Dribble() { Actor ball; ball=getOneObjectAtOffset(0,10,ball.class); if (ball !=null && Greenfoot.isKeyDown("space") ) { setLocation(getX()+4, getY()); //For the basic solution this is where you put your new code. } }
You need to login to post a reply.