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

2020/4/16

Can't get enemies to move

Ryaj987 Ryaj987

2020/4/16

#
Hi, I'm working on a platformer and I can't get the enemies to move. This the code in the class itself import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) public class Skeleton extends Enemies { int mapX; int mapY; int frame = 0; private int animationCount; private int vSpeed = 0; private int acceleration = 2; private int spriteHeight = getImage().getHeight(); private int spriteWidth = getImage().getWidth(); private int lookForGroundDistance = (int)spriteHeight/2; private int lookForEdge = (int)spriteWidth/2; private int speed = 1; public Skeleton(int mapXIn, int mapYIn) { mapX = mapXIn; mapY = mapYIn; } public void act() { checkFall(); move(); } public void edgeDetection() { if(getX() < 10 || getX() > getWorld().getWidth() - 10) { speed *= -1; } } public void move() { Actor platform = getOneObjectAtOffset(lookForEdge, lookForGroundDistance, Platform.class); if(platform == null) { speed *= -1; lookForEdge *= -1; } else { move(speed); } } public void fall() { vSpeed += 1; if (vSpeed >= 10) { vSpeed = 10; } int dir=(int)Math.signum(vSpeed); for(int step=0; step!=vSpeed; step+=dir) { setLocation(getX(), getY()+dir); if(onPlatform()) { setLocation(getX(), getY() - dir); vSpeed = 0; break; } } } public boolean onPlatform() { Actor under = getOneObjectAtOffset(0, getImage().getHeight() / 2 , Platform.class); return under != null; } public boolean checkRightWalls() { int spriteWidth = getImage().getWidth(); int xDistance = (int)(spriteWidth/2); Actor rightWall = getOneObjectAtOffset(xDistance, 0, Platform.class); if(rightWall == null) { return false; } else { stopByRightWall(rightWall); return true; } } public void stopByRightWall(Actor rightWall) { int wallWidth = rightWall.getImage().getWidth(); int newX = rightWall.getX() - (wallWidth + getImage().getWidth())/2; setLocation(newX - 5, getY()); speed *= -1; } public boolean checkLeftWalls() { int spriteWidth = getImage().getWidth(); int xDistance = (int)(spriteWidth/-2); Actor leftWall = getOneObjectAtOffset(xDistance, 0, Platform.class); if(leftWall == null) { return false; } else { stopByLeftWall(leftWall); return true; } } public void stopByLeftWall(Actor leftWall) { int wallWidth = leftWall.getImage().getWidth(); int newX = leftWall.getX() + (wallWidth + getImage().getWidth())/2; setLocation(newX + 5, getY()); speed *= -1; } public void checkFall() { if(onPlatform()) { vSpeed = 0; } else { fall(); } } }
danpost danpost

2020/4/16

#
I wonder if this change in onPlatform will help:
public boolean onPlatform() {
    setLocation(getX(), getY()+1);
    Actor under = getOneObjectAtOffset(0, getImage().getHeight() / 2 , Platform.class);
    setLocation(getX(), getY()-1);
    return under != null;
}
Ryaj987 Ryaj987

2020/4/16

#
That did something, but still didn't get it to work. Its a scrolling game, and I'm using parts of code from a game that doesn't scroll. Is there anyway to share the whole program without copy and pasting everything into this?
Ryaj987 Ryaj987

2020/4/16

#
Also, you're the man! You helped me with my last issue too.
danpost danpost

2020/4/16

#
Ryaj987 wrote...
Is there anyway to share the whole program without copy and pasting everything into this?
You can Share it to this site -- making sure you check the Publish source code box.
Ryaj987 Ryaj987

2020/4/16

#
I shared it
danpost danpost

2020/4/16

#
Ryaj987 wrote...
I shared it
I got it already.
Ryaj987 Ryaj987

2020/4/16

#
Something simple?
danpost danpost

2020/4/16

#
Ryaj987 wrote...
Something simple?
I meant that I got the scenario.
Ryaj987 wrote...
Something simple?
Maybe. I think that by the way your scrolling system is using the mapX and mapY values of the enemies, it is negating any movement they take on their own. Maybe you just need to have the enemies change their mapX and mapY values when they move. Hopefully that will resolve the issue.
Ryaj987 Ryaj987

2020/4/16

#
I tried that and they just ghost off the screen. I might just have to retool how I'm putting them into the map. rip
danpost danpost

2020/4/16

#
Ryaj987 wrote...
I tried that and they just ghost off the screen. I might just have to retool how I'm putting them into the map. rip
I got them to move a little with this:
public void act() 
{
    int x = getX(), y = getY();
    fall();
    move();
    mapX += getX()-x;
    mapY += getY()-y;
}
You need to login to post a reply.