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

2014/12/23

replace object with another object

bladerunner2nd bladerunner2nd

2014/12/23

#
Hi @ all! I'm completely new to green foot, just started one week ago and I want to make a little game. So you are a fish and eat other fishes, the "dead" fish should change to a bloodstain and move to the left side, just as the normal fish would have done, if you wouldn't have eaten it. So my code works, but not replacing the fish object with the bloodstain object. And the fish shouldn't just change image, because then you would still be able to kill it. I want it to be an object, that just does nothing expect leaving the screen like the normal fish. Here is my code: public void eat() // kills every eatable fish { Actor fish; fish = getOneObjectAtOffset (0,0, Fishes.class); if (fish != null) { World world; world = getWorld(); Waterworld waterworld = (Waterworld)world; Counter counter = waterworld.getCounter(); counter.addScore(); world.removeObject (fish); BloodStain bloodstain = new BloodStain(); world.addObject (bloodstain); fishEaten++; Greenfoot.playSound("BloodSplatter.mp3"); } } PLEASE help me, I couldn't find a solution and I have no idea. Maybe you could write the code. Thank you for any help! :)
danpost danpost

2014/12/24

#
First, what classes do you have and what class do each one of them extend?
bladerunner2nd bladerunner2nd

2014/12/24

#
fishes extends actor piranha1 extends piranhas piranhas extend actor bloodstain extends actor thanks!
danpost danpost

2014/12/24

#
I would think a piranha would be considered a fish and therefore it should extend fishes -- not that this is such big deal, however. I do now see something in you original code posting that is a bit off and you should probably have mentioned you were getting an error message saying something like "method 'addObject(Actor)' not found" or maybe something about wrong number of argument. This line:
1
world.addObject (bloodstain);
cannot compile as is. The 'addObject' method of the World class uses one Actor object (the actor to add into the world) and two ints (the x and y coordinates where the actor is to be added at). You are missing the location at which the actor is to be placed. When you get a message like that, search the class documentation for the method and see how it should be used.
bladerunner2nd bladerunner2nd

2014/12/24

#
yep, but in the fishes class are just the prey fishes; ^^ without BloodStain bloodstain = new BloodStain(); world.addObject (bloodstain); it works fine, but i just want that the bloodstain appears one the same place as the fish who just disappeared; is that possible?
danpost danpost

2014/12/24

#
You can apply any public method to the variable 'fish' which is of Actor type; but, for its location, it must be done prior to removing the 'fish' from the world.
davmac davmac

2014/12/24

#
it works fine, but i just want that the bloodstain appears one the same place as the fish who just disappeared; is that possible?
Yes. You must supply the appropriate location (X and Y coordinates) as parameters to the 'addObject' method. The bloodstain will then be added at these coordinates.
bladerunner2nd bladerunner2nd

2014/12/25

#
thanks for your help; but thats the problem that i don't know the exact location because it always depends on where you eat the fish. at exactly that point, the bloodstains should appear; how can i manage to do that? btw: merry xmas! :)
danpost danpost

2014/12/25

#
Use the pre-defined method from the Actor class to get the values you need. See the Actor class documentation and review what methods are available if you still are not sure what to do.
davmac davmac

2014/12/25

#
bladerunner2nd wrote...
thanks for your help; but thats the problem that i don't know the exact location because it always depends on where you eat the fish. at exactly that point, the bloodstains should appear; how can i manage to do that? btw: merry xmas! :)
Merry Christmas! I feel like this needs further explication. We are trying to lead you to the solution on your own but I'm not sure you are really understanding. So, let me try again: You need to add one object at the location of another. You say that you can't specify the co-ordinates to add the object because you don't know what they are. However, you don't need to provide literal values such as '124' or '57'; you can use expressions which evaluate to a number. In this case, you want to use two expressions - one for the x coordinate and one for the y coordinate. What I mean by 'expression' is 'something that gives a value'. For example, a method call usually qualifies as an expression. You can use documented methods to retrieve the x and y co-ordinates of either the fish or the object which is doing the eating, and use the resulting values as parameters to the world's 'addObject' method. If you're still confused, let me start with a simpler problem which forms part of the larger one you're now dealing with: how can you get the co-ordinates of the fish, i.e. which method would you call (look it up in the documentation if you don't know) - and how do you write that method call? If you can answer this, we can guide you to the next step.
bladerunner2nd bladerunner2nd

2014/12/27

#
thanks; so i managed to do a bloodstain moving from right to left and kind of replace the fish; i get no syntax errors but if i eat a fish, i get an error code. thats the code of the fish to be eaten: public void die() { Actor piranhas = getOneIntersectingObject(Piranhas.class); if(piranhas!= null) { Greenfoot.playSound("BloodSplatter.mp3"); World world; world = getWorld(); Waterworld waterworld = (Waterworld)world; Counter counter = waterworld.getCounter(); counter.addScore(); getWorld().removeObject(this); getWorld().addObject(new BloodStain(), getX(), getY()); } } and thats the code of the bloodstain (sorry its a bit long): public class BloodStain extends Actor { GreenfootImage bloodstain1 = new GreenfootImage("bloodstain1(klein).png"); GreenfootImage bloodstain2 = new GreenfootImage("bloodstain2(klein).png"); GreenfootImage bloodstain3 = new GreenfootImage("bloodstain3(klein).png"); GreenfootImage bloodstain4 = new GreenfootImage("bloodstain4(klein).png"); GreenfootImage bloodstain5 = new GreenfootImage("bloodstain5(klein).png"); GreenfootImage bloodstain6 = new GreenfootImage("bloodstain6(klein).png"); GreenfootImage bloodstain7 = new GreenfootImage("bloodstain7(klein).png"); GreenfootImage bloodstain8 = new GreenfootImage("bloodstain8(klein).png"); public int animationCounter; public int frame = 1; public void act() { animationCounter = animationCounter + 1; if(animationCounter % 4 == 0) { animation(); } setLocation (getX()-2, getY()); // ohne das funktioniert es (fast) immer } public void animation() { if(frame == 1) { setImage(bloodstain1); frame = 2; } else if(frame == 2) { setImage(bloodstain1); frame = 3; } else if(frame == 3) { setImage(bloodstain2); frame = 4; } else if(frame == 4) { setImage(bloodstain2); frame = 5; } else if(frame == 5) { setImage(bloodstain3); frame = 6; } else if(frame == 6) { setImage(bloodstain3); frame = 7; } else if(frame == 7) { setImage(bloodstain4); frame = 8; } else if(frame == 8) { setImage(bloodstain4); frame = 9; } else if(frame == 9) { setImage(bloodstain5); frame = 10; } else if(frame == 10) { setImage(bloodstain5); frame = 11; } else if(frame == 11) { setImage(bloodstain6); frame = 12; } else if(frame == 12) { setImage(bloodstain6); frame = 13; } else if(frame == 13) { setImage(bloodstain7); frame = 14; } else if(frame == 14) { setImage(bloodstain7); frame = 15; } else if(frame == 15) { setImage(bloodstain8); frame = 16; } else if(frame == 16) { setImage(bloodstain8); frame = 17; } else if(frame == 17) { getWorld().removeObject(this); } } } and thats the error message i get: java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed. at BabyNemo.die(BabyNemo.java:59) at BabyNemo.act(BabyNemo.java:18) at BloodStain.act(BloodStain.java:26) i have different codes for removing the fish: for example if you eat it or it just reaches the left corner of the game; and the bloodstain should disappear after the animation or if it reaches the left corner too; i don't just want one removing code
davmac davmac

2014/12/27

#
So, read the error message:
Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed.
Looking at your code, this seems right. You've got this:
1
2
getWorld().removeObject(this);
getWorld().addObject(new BloodStain(), getX(), getY());
It does exactly what the error says: removes an object (this) from the world, and then asks for its position (getX(), getY()) when it is no longer in the world. The fix is obvious: reverse the order of the two statements. By the way, when you post here, there are a bunch of links below the text box that you type into. One of them says: Posting code? read this! Seeing as you are posting code, you need to click that link. It explains how you should use 'code' tags to properly format the code.
danpost danpost

2014/12/27

#
bladerunner2nd wrote...
i have different codes for removing the fish: for example if you eat it or it just reaches the left corner of the game; and the bloodstain should disappear after the animation or if it reaches the left corner too; i don't just want one removing code
Well, it can be done both ways -- as two separate removing codes or as two separate conditions within the same 'if' that removes the actor. That is:
1
2
3
4
if (/**  condition one */) /** remove actor */
if (/** still in world */ && /** condition two */) /** remove actor */
// which is equivalent to the following
if (/** condition one */ || /** condition two */) /** remove actor */
In the second way, condition two will not be checked if condition one was true -- so, no exceptions will be thrown and the extra check to ensure the actor is still in the world to check the second condition is not needed (since both checks are done prior to the actor being removed). Hope that made some sense.
You need to login to post a reply.