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

2011/12/21

Game trouble

1
2
3
4
jdemshki jdemshki

2011/12/21

#
So, I'm working on this game and i have my bombs being removed from the world if they hit the edge of the world or a castle. this was working fine but then i put in more code to change the size of the bomb and now they won't disappear when they hit a castle. if someone could look at my code and offer some help i would be mega happy. thanks! public void disappear() {Actor castle; castle = getOneObjectAtOffset(0, 0, Castle.class) ; Actor bomb; bomb = getOneObjectAtOffset(0, 0, Bomb.class) ; if (castle != null) { World world; world = getWorld(); world.removeObject(castle); world.removeObject(bomb); } if (this.atWorldEdge()) { World world; world = getWorld(); world.removeObject(this); } }
kiarocks kiarocks

2011/12/21

#
can i see other related code? There is no problem here.
danpost danpost

2011/12/21

#
What is happening is if a castle is hit, you are removing *another* bomb (which is never there); getOneObjectAtOffset never returns the object that calls the method. What you need to do is: (1) Remove the lines 'Actor bomb;' and 'bomb = getOneObjectAtOffset(0, 0, Bomb.class);'. (2) Change the line 'world.removeObject(bomb);' to 'world.removeObject(this);', and (3) immediately following 'world.removeObject(this);' referenced in (2) above, add the line 'return;'. Step (3) is neccessary to avoid errors that would occur if the rest of the method was allowed to execute. Of course, if removing 'this' was the last thing done in the method, you would not have to force a return.
jdemshki jdemshki

2011/12/21

#
totally perfect. what is weird is that it worked before this until i scaled the bomb, it all worked perfectly.
kiarocks kiarocks

2011/12/21

#
You were just lucky.
jdemshki jdemshki

2011/12/21

#
i hate to keep bothering you but you always respond. i want to make something that can block the bombs from hitting the castles. i have all the castles on one side and I'm going to have a rectangular block going up and down. how do you get something to move vertically. when i put in the common move code it just goes to the right,
danpost danpost

2011/12/21

#
You might have to take the image and rotate(270), and also take the actor and setRotation(90). Move follows the setRotation value.
jdemshki jdemshki

2011/12/21

#
public class Blocker extends Mover { public Blocker() { //image size GreenfootImage img = getImage(); int width = 80; int height = 40; img.scale (width, height); setRotation(90);} /** * Act - do whatever the Blocker wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { move(10); if (this.atWorldEdge()) {setRotation(270); } } } i got it to move down and when it hits the bottom to rotate and move up, but when it hits the top it just stops. any ideas?
kiarocks kiarocks

2011/12/21

#
it sets the rotation to be 270 like it was before, so it is still in the same direction.
kiarocks kiarocks

2011/12/21

#
you might use
setRotation(getRotation-180);
jdemshki jdemshki

2011/12/22

#
i put that in place of the 270 and it said cannot find symbol - getRotation
Royalblue64 Royalblue64

2011/12/22

#
you need to do setRotation(getRotation() - 180);
jdemshki jdemshki

2011/12/22

#
that worked!!! thanks! can you explain why though? just so like I can understand
Royalblue64 Royalblue64

2011/12/22

#
I think what you're asking is why getRotation() works and getRotation does not. The answer to that is that in the Actor class there is a method called getRotation() already defined, so you have to use that. Since it is a method and not a variable, you need to put the () for any instance fields (for variable input) that there might be.
jdemshki jdemshki

2011/12/22

#
i want to make one actor to be removed when it hits the other. its a bomb hitting like a wall (I'm calling it a blocker). what would be the easiest way to do this?
There are more replies on the next page.
1
2
3
4