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

2013/10/4

The code is removing the object but how to replace the object with another object or with another image

JERRY**5 JERRY**5

2013/10/4

#
public void die() { Actor Man = getOneObjectAtOffset(0, 0, Man.class); if (Man != null) { getWorld().removeObject(Man); Greenfoot.playSound("aah.wav"); }
danpost danpost

2013/10/4

#
You can use 'getWorld().addObject(new ActorClassName(), getX(), getY())' before removing the Man object. BTW, it must be 'before' or 'getWorld' will be 'null' and getX() will error.
JERRY**5 JERRY**5

2013/10/4

#
when the ball touches the man the the man disappears but when that ball exits the terminal opens and the below line is giving error Actor Man = getOneObjectAtOffset(0, 0, Man.class);
danpost danpost

2013/10/4

#
The error is probably found in your act method. You are probably calling 'die' after a call to another method that removes the ball from the world when it hits an edge of the world. It this is so, then instead of 'die();' in the act method, use 'if (getWorld() != null) die();'. This ensures the ball is still in the world before calling 'die'.
JERRY**5 JERRY**5

2013/10/4

#
If the ball falls on man the image changes to blood but a new man gets created itself and the game continues
JERRY**5 JERRY**5

2013/10/4

#
Man class public void act() { if(Greenfoot.mouseMoved(null)) { MouseInfo mouse = Greenfoot.getMouseInfo(); setLocation(mouse.getX(),getY()); } } public void die() { Actor ball = getOneObjectAtOffset(0, 0, Ball3.class); if(ball != null) { getWorld().removeObject(ball); //getWorld().addObject(new Blood(),getX(),getY()); } }
JERRY**5 JERRY**5

2013/10/4

#
Ball2 Class public Ball2(int dir) { direction = dir; // save horizontal direction to move } public void act() { fallingballs2(); if (getWorld() != null) die(); //die(); } public void fallingballs2() { //CODE GIVEN BY DANPOST setLocation(getX(), getY()+(++vSpeed)); // move vertically adding gravity if (getY() >= getWorld().getHeight()-getImage().getHeight()/2 && vSpeed > 0) vSpeed = -vSpeed; // check height, bounce setLocation(getX()+direction*hSpeed, getY()); // move horizontally if (getX()-(getWorld().getWidth()-1)*(direction+1)/2 == 0) getWorld().removeObject(this); // check edge, remove //Greenfoot.playSound("Onebounce.mp3"); } public void die() { Actor Man = getOneObjectAtOffset(0, 0, Man.class); if (Man != null) { getWorld().removeObject(Man); Greenfoot.playSound("aah.wav"); getWorld().addObject(new Blood(), getX(), getY()); } }
danpost danpost

2013/10/4

#
Let me get a better understanding of what you want. The man is to collect Ball3 objects (when nothing, as yet, special happens) and avoid Ball2 objects (when man is removed and blood is to replace him). This is at least what I am getting from the code you have shown. Is this correct?
JERRY**5 JERRY**5

2013/10/4

#
Its like a windows 8 falling balls game in which the balls falls randomly and if the balls falls on him he will die
danpost danpost

2013/10/4

#
Telling me 'Its like a windows 8 falling balls game' does not mean anything to me (I am using Windows 7). How are Ball2 objects different from Ball3 objects?
JERRY**5 JERRY**5

2013/10/5

#
they are same anly the size of the ball is different
danpost danpost

2013/10/5

#
Again, it would be best to put the code for detecting any ball object in the Man class.
Actor ball2 = getOneObjectAtOffset(0, 0, Ball2.class);
Actor ball3 = getOneObjectAtOffset(0, 0, Ball3.class);
if (ball2 != null || ball3 != null)
{
    getWorld().addObject(new Blood(), getX(), getY());
    getWorld().removeObject(this);
    Greenfoot.playSound("aah.wav");
}
Remove any checking for man and calls to 'die' (including 'if (getWorld() != null') from the ball classes. Also, you need to call 'die' from the act method of the Man class after placing the code in the 'die'' method in the Man class or include the code itself in the act method of the Man class. You should probably only have one Ball class. How are you deciding which size ball to create, when? and how many different sized balls do you have?
JERRY**5 JERRY**5

2013/10/5

#
Thanks
You need to login to post a reply.