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

2015/3/3

double object at world edge

Jeph Jeph

2015/3/3

#
the coding for:if an object is at world edge add new object from the current object location --e.g object is a "ball"
fejfo fejfo

2015/3/3

#
but in the act of to object
if(isAtEdge) {
    getWorld().addObject(ball,getX(),getY());
}
danpost danpost

2015/3/4

#
Jeph wrote...
the coding for:if an object is at world edge add new object from the current object location --e.g object is a "ball"
The suggestion given by fejfo has these issues: * 'isAtEdge' should be 'isAtEdge()' as it is a method call, not a field value * 'ball' should be 'new Ball()' (presuming 'Ball' is the name of the class that the "ball" is created from) * the ball hitting the edge of the world needs to be directed back away from the wall * the ball created also needs to be directed back away from the wall using a different angle, a different speed, or both else one ball will hide the other * both balls, the one hitting the wall and the one created, need to moved back off the wall else multiple new balls will be created
Jeph Jeph

2015/3/4

#
Ok as a side note am using the joy of code#17 bouncy coloured ball.@ the moment my ball just turns its direction and image @ world edge is there another way to get a new ball when the current ball is at world edge? and limit it to <5
LordLuhar LordLuhar

2015/3/4

#
What you could do is you could put This code in your Ball code
int i = 1; // Declare this variable

Public void act
{
SpawnBall();
}
Public class SpawnBall()
{
if ( i<=5 && isAtEdge())
{
World world=getWorld();
world.addObject( new Ball(), (//put x here//),(//put y here//));
i++;
}
}
danpost danpost

2015/3/4

#
You can limit the number of balls in the world by adding an if condition to the spawning of new balls (if the number of balls in the world is less than 4, or 5). As far as there being another way to add new balls into the world when one reaches a world edge, not really. @LordLuhar * the int field 'i' would be unique to each ball, so each ball will then know how many other balls that it has spawned; * 'Public' in line 3 should be 'public', starting with a lowercase 'p' (same on line 7); * 'class' in line 7 should be 'void' (there is no value returned from the method); * method names should, by convention, begin with a lowercase letter ('SpawnBall' should be 'spawnBall');
You need to login to post a reply.