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

2013/5/12

CODE HELP PLZ

1
2
SWAG SWAG

2013/5/12

#
I want a code where if my player jumps on top of the enemies he is removed.
danpost danpost

2013/5/12

#
(1) when you say 'jumps on top of the enemies', do you mean intersects any enemy object? (2) do you at least know what code to use within the Player class to remove the player object?
SWAG SWAG

2013/5/12

#
danpost wrote...
(1) when you say 'jumps on top of the enemies', do you mean intersects any enemy object? (2) do you at least know what code to use within the Player class to remove the player object?
When I mean jump on top I mean he hits the top half of the enemies. Also I know how to remove it.
danpost danpost

2013/5/12

#
You just need to create the proper condition checks. Maybe set up a 'hit-box' using coordinates with respect to the intersecting enemy's location. Like, if the intersecting enemy was at (x, y), what range with respect to x and y should your player's location coordinates be, to be removed. It would really depend on how restrictive you want to be; but, for an example, one way is that the player's x-coordinate has to be between 'x-enemy.getImage().getWidth()/2' and 'x+enemy.getImage().getWidth()/2', and the player's y-coordinate must be less than that of the enemy's.
SWAG SWAG

2013/5/13

#
danpost wrote...
You just need to create the proper condition checks. Maybe set up a 'hit-box' using coordinates with respect to the intersecting enemy's location. Like, if the intersecting enemy was at (x, y), what range with respect to x and y should your player's location coordinates be, to be removed. It would really depend on how restrictive you want to be; but, for an example, one way is that the player's x-coordinate has to be between 'x-enemy.getImage().getWidth()/2' and 'x+enemy.getImage().getWidth()/2', and the player's y-coordinate must be less than that of the enemy's.
could you post a code, so if my player touches the top of the enemies ......
danpost danpost

2013/5/13

#
I practically gave you the code above:
Actor enemy = getOneIntersectingObject(Enemy.class);
if (getX() > enemy.getX()-enemy.getImage().getWidth()/2 &&
    getX() < enemy.getX()+enemy.getImage().getWidth()/2 &&
    getY() < enemy.getY())
        getWorld().removeObject(this);
SWAG SWAG

2013/5/13

#
danpost wrote...
I practically gave you the code above:
Actor enemy = getOneIntersectingObject(Enemy.class);
if (getX() > enemy.getX()-enemy.getImage().getWidth()/2 &&
    getX() < enemy.getX()+enemy.getImage().getWidth()/2 &&
    getY() < enemy.getY())
        getWorld().removeObject(this);
Thank so far, but I was wonder if its possible when player hits it sets a image "down.png" for 15 seconds on the enemies and the enemies can't move then after 15 seconds the enemies return to normal.
danpost danpost

2013/5/13

#
Do you want just the hit enemy to change image and freeze, or all enemies at once?
SWAG SWAG

2013/5/13

#
danpost wrote...
danpost danpost

2013/5/13

#
Add a 'stunTimer' in the Enemy class as follows:
// add instance int field
private int stunTimer = 0;
// start your act method with
public void act()
{
    if (stunTimer > 0) // if frozen
    {
        stunTimer--;
        if (stunTimer > 0) return; // if timer not done, exit method
        setImage("up.png"); // restore normal image
    }
    // rest of act method
}
// when enemy hit, do the following
setImage("down.png");
stunTimer = 900;
SWAG SWAG

2013/5/13

#
danpost wrote...
Add a 'stunTimer' in the Enemy class as follows:
// add instance int field
private int stunTimer = 0;
// start your act method with
public void act()
{
    if (stunTimer > 0) // if frozen
    {
        stunTimer--;
        if (stunTimer > 0) return; // if timer not done, exit method
        setImage("up.png"); // restore normal image
    }
    // rest of act method
}
// when enemy hit, do the following
setImage("down.png");
stunTimer = 900;
my enemy is hit in the player class. Also my enemy moves and I don't want him to move if hit on top.
danpost danpost

2013/5/13

#
Then, in the Enemy class, make a method to call when hit is detected in the player class.
public void setStunned()
{
    setImage("down.png");
    stunTimer = 900;
}
Call this method from the player class when hit.
Enemy enemy = (Enemy) getOneIntersectingObject(Enemy.class);
if (enemy != null &&
    getX() > enemy.getX()-enemy.getImage().getWidth()/2 &&
    getX() < enemy.getX()+enemy.getImage().getWidth()/2 &&
    getY() < enemy.getY())
        enemy.setStunned();
SWAG SWAG

2013/5/13

#
danpost wrote...
Then, in the Enemy class, make a method to call when hit is detected in the player class.
public void setStunned()
{
    setImage("down.png");
    stunTimer = 900;
}
Call this method from the player class when hit.
Enemy enemy = (Enemy) getOneIntersectingObject(Enemy.class);
if (enemy != null &&
    getX() > enemy.getX()-enemy.getImage().getWidth()/2 &&
    getX() < enemy.getX()+enemy.getImage().getWidth()/2 &&
    getY() < enemy.getY())
        enemy.setStunned();
I have a couple of questions. For my enemy there is not image "up". It all in my move(); and if my put move(); where you put image up my enemy doesn't move. Also "cannot find symbol - method setStunned()" in my player class.
danpost danpost

2013/5/13

#
First off, I do not know what the image filename is for the initial image of the Enemy, so I used that because the stunned image is "down.png". You have to change "up.png" to whatever image you are using when the enemy is moving. You obviously did not put things where I said to put them. The 'setStunned' method should be in the Enemy class. The last code-set (starting 'Enemy enemy = ...') should go in the Player class (it is what detects the player on top of an enemy and stuns it). You should not be getting that error message in the player class. If you put the field declaration for 'stunTimer' in the Enemy class and started the 'act' method in the Enemy class as I stated above, your enemy should only stop moving when it is stunned.
SWAG SWAG

2013/5/13

#
danpost wrote...
First off, I do not know what the image filename is for the initial image of the Enemy, so I used that because the stunned image is "down.png". You have to change "up.png" to whatever image you are using when the enemy is moving. You obviously did not put things where I said to put them. The 'setStunned' method should be in the Enemy class. The last code-set (starting 'Enemy enemy = ...') should go in the Player class (it is what detects the player on top of an enemy and stuns it). You should not be getting that error message in the player class. If you put the field declaration for 'stunTimer' in the Enemy class and started the 'act' method in the Enemy class as I stated above, your enemy should only stop moving when it is stunned.
Now the only problem is the picture doesn't stay. I hit it and picture comes once and then goes away.
There are more replies on the next page.
1
2