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

2016/2/13

Objects Falling

spider65 spider65

2016/2/13

#
How do I make Objects fall down the same y axis but do randomized objects?
danpost danpost

2016/2/13

#
Choose a random number among the number of different object you may have falling. Then use a switch statement to create an Actor object represented by that chosen number:
Actor actor = null;
switch (Greenfoot.getRandomNumber(3))
{
    case 0: actor = new ActorA(); break;
    case 1: actor = new ActorB(); break;
    case 2: actor = new ActorC(); break;
}
addObject(actor, ...
spider65 spider65

2016/2/14

#
How do I make someone go left and right along the x axis but random lengths of each move?
danpost danpost

2016/2/14

#
spider65 wrote...
How do I make someone go left and right along the x axis but random lengths of each move?
You could alternate the movement as you might be suggesting; however, if you just choose a new x-coordinate to move to, then, each time one is reached, the actor movement will be slightly improved (that is, even if two chosen x-coordinate values make the actor move in the same direction, it would be like the second chosen coordinate was chosen initially). This, in turn, would reduce shorter wiggle movements. You will probably need to use the 'addedToWorld' method in the class of the actor to set its initial x-coordinate destination. You can use the 'Math.signum' method on the difference in the current and destination coordinate to determine the direction to move. Refer to the documentation of the java.lang.Math class if you are not familiar with that method.
spider65 spider65

2016/2/14

#
Can you explain all this in simpler terms?
danpost danpost

2016/2/14

#
Add an instance int field into the class of the actor to hold a destination x coordinate at which the actor is to move to:
// outside of any method (but within the class)
private int xDestination;
Now, the value of this field needs to be initially set and then reset when the actor reaches the destination x-coordinate held by the field. Since it needs to be done at two different places, a separate method should be built for setting its value:
public void setXDestination()
{
    xDestination = Greenfoot.getRandomNumber(getWorld().getWidth());
}
This method can now be called from the 'addedToWorld' method and from the code that determines that a destination has been reached. The destination has been reached when the expression 'xDestination-getX()' equals zero. If it evaluates to a value greater than zero, the actor should move right and if less than zero, the actor should move left. As an alternative to calling the method from the 'addedToWorld' method, you can call it immediately after adding the object into the world. For example:
ActorSubclassName whatever = new ActorSubclassName();
addObject(whatever, < x, y >); // wherever
whatever.setXDestination();
spider65 spider65

2016/2/15

#
Can you just give me the code because I still do not get what you are saying.
danpost danpost

2016/2/15

#
spider65 wrote...
Can you just give me the code because I still do not get what you are saying.
I think I have supplied all the code necessary -- maybe not all in code form; but very little of what is needed was not supplied in code form -- and even that was spelled out pretty clearly.
You need to login to post a reply.