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

2016/10/21

problem

Nosson1459 Nosson1459

2016/10/21

#
import greenfoot.*;

/**
 * A little penguin that wants to get to the other side.
 */
public class Pengu extends Mover
{
    private static final int jumpStrength = 16;
    
    public void act() 
    {
        changeMove();
        coins();
        checkKeys();        
        checkFall();
        
    }
    
    private void checkKeys()
    {
        Scene scene=(Scene)getWorld();
        if (Greenfoot.isKeyDown("left") )
        {
            setImage("pengu-left2.png");
            if(!touchingLeftSide()||!touchingLeftSide2()||!touchingLeftSide3())
               moveLeft();
        }
        if(touchingRightSide()||touchingRightSide2()||touchingRightSide3())
        {
            scene.move=false;
        }
        else if(getX()>=342)
        {
            scene.move=true;
        }
        if (Greenfoot.isKeyDown("right")&&scene.move==false)
        {
            setImage("pengu-right2.png");
            
            if(!touchingRightSide()||!touchingRightSide2()||!touchingRightSide3())
               moveRight();
        }
        if (Greenfoot.isKeyDown("space") )
        {
            if (onGround())
                jump();
        }
    }    
    
    private void jump()
    {
        setVSpeed(-jumpStrength);
        fall();
    }
    
    private void checkFall()
    {
        if (onGround()) {
            setVSpeed(0);
        }
        else {
            fall();
        }
        
    }
    private void coins()
    {
        Scene scene=(Scene)getWorld();
        Coin coin=(Coin)getOneIntersectingObject(Coin.class);
        if(coin!=null)
        {
            scene.removeObject(coin);
            scene.score=scene.score+100;
            scene.coins=scene.coins+1;
        }
    }
    private void changeMove()
    {
        Scene scene=(Scene)getWorld();
        if(getX()>=342)
        {
            scene.move=true;
        }
        else
        {
            scene.move=false;
        }
    }
}
import greenfoot.*;

/**
 * The class Mover provides some basic movement methods. Use this as a superclass
 * for other actors that should be able to move left and right, jump up and fall 
 * down.
 */
public class Mover extends Actor
{
    private static final int acceleration = 2;      // down (gravity)
    private static final int speed = 7;             // running speed (sideways)
    
    private int vSpeed = 0;                         // current vertical speed
    

    public void moveRight()
    {
        setLocation ( getX() + speed, getY() );
    }

    public void moveLeft()
    {
        setLocation ( getX() - speed, getY() );
    }
            
    public boolean onTop()
    {
        Object under = getOneObjectAtOffset(0, -getImage().getHeight()/2 , Boxes.class);
        return under != null;
    }
    
    public boolean onGround()
    {
        Object under = getOneObjectAtOffset(0, getImage().getHeight()/2 , null);
        return under != null;
    }
    
    public boolean touchingLeftSide()
    {
       Object side=getOneObjectAtOffset(-getImage().getWidth()/2,getImage().getHeight()/2-1,null);
       return side!=null;
    }
    public boolean touchingLeftSide2()
    {
       Object side=getOneObjectAtOffset(-getImage().getWidth()/2,-getImage().getHeight()/2+1,null);
       return side!=null;
    }
    public boolean touchingLeftSide3()
    {
       Object side=getOneObjectAtOffset(-getImage().getWidth()/2,0,null);
       return side!=null;
    }
    
    public boolean touchingRightSide()
    {
       Object side=getOneObjectAtOffset(getImage().getWidth()/2,-getImage().getHeight()/2+1,null);
       return side!=null;
    }
    public boolean touchingRightSide2()
    {
       Object side=getOneObjectAtOffset(getImage().getWidth()/2,getImage().getHeight()/2-1,null);
       return side!=null;
    }
    public boolean touchingRightSide3()
    {
       Object side=getOneObjectAtOffset(getImage().getWidth()/2,0,null);
       return side!=null;
    }
    
    public void setVSpeed(int speed)
    {
        vSpeed = speed;
    }
    
    public void fall()
    {
        bump();
        setLocation ( getX(), getY() + vSpeed);
        vSpeed = vSpeed + acceleration;
        if ( atBottom() )
            gameEnd();
    }
    
    private boolean atBottom()
    {
        return getY() >= getWorld().getHeight() - 2;
    }
    
    public void gameEnd()
    {
        Scene scene=(Scene)getWorld();
        scene.lives--;
        scene.reset();
        scene.level1();
    }

    public void bump()
    {
        if (onTop())
        {
            vSpeed=-vSpeed;
        }
    }
}
import greenfoot.*; 
import java.util.List;
/**
 * This is the whole scene. It creates and contains the objects that are in it.
 */
public class Scene extends World
{
    public int turnLeft;
    public int turnRight;
    public int lives=2;
    private int width=750;
    private int height=500;
    public int score=0;
    public int coins=0;
    public int level=1;
    public int addObjects;
    public int objectNumber;
    public boolean move=false;
    public Scene()
    {    
        super(934, 500, 1);    // define size and resolution
        setPaintOrder(HelpBox.class,Blocker.class,GameOver.class,Help.class,Coins.class,Level.class,Life.class,Score.class,Bar.class,Pengu.class,Snake.class,SnakeD.class,Cloud.class);
        addObject(new Bar(),467,18);
        addObject(new Coins("Coins: "),787,18);
        addObject(new Life("Lives: "),157,18);
        addObject(new Score("Score: "),592,18);
        addObject(new Level("Level: "),362,18);
        addObject(new Blocker(),46,250);
        addObject(new Blocker(),888,250);
        level2();
    }
    public void act()
    {
        if(lives<0)
        {
            addObject(new GameOver(),375,250);
            if(score==0)
            {
                showText(""+score,365,210);
            }
            else if(score>99&&score<1000)
            {
                showText(""+score,375,210);
            }
            else if(score>999&&score<10000)
            {
                showText(""+score,380,210);
            }
            else if(score>9999&&score<100000)
            {
                showText(""+score,385,210);
            }
            else if(score>99999)
            {
                showText(""+score,390,210);
            }
            Greenfoot.stop();
        }
        if(coins==100)
        {
            score+=500;
            lives++;
            coins=0;
        }
        if(level==2)
        {
            reset();
            level2();
        }
    }
    public void reset()
    {
        List<Platform> platform=getObjects(Platform.class);
        List<Box> box=getObjects(Box.class);
        List<Pengu> pengu=getObjects(Pengu.class);
        List<cliffC> cliffC=getObjects(cliffC.class);
        List<CliffA> cliffA=getObjects(CliffA.class);
        List<Cloud> cloud=getObjects(Cloud.class);
        List<Monster> monster=getObjects(Monster.class);
        List<Block> block=getObjects(Block.class);
        removeObjects(platform);
        removeObjects(box);
        removeObjects(pengu);
        removeObjects(cliffC);
        removeObjects(cliffA);
        removeObjects(cloud);
        removeObjects(monster);
        removeObjects(block);
    }
    public void level1()
    {
        addObject ( new CliffA(), 50, 405);
        addObject ( new cliffC(), 650, 405);
        
        addObject ( new Cloud(), 369, 315 );
        
        addObject ( new Pengu(), 46, 291 );
        
        addObject(new SnakeD(),650,302);
        addObject(new Block(),141,270);
        addObject(new Block(),141,310);
        addObject(new Block(),181,310);
        addObject(new Block(),221,310);
        addObject(new Block(),261,310);
        addObject(new Block(),261,270);
        addObject(new Snake(),201,275);
    }
    public void level2()
    {
        for(int j=112;j<573;j+=40)
        {
            for(int i=360;i<481;i+=40)
            {
                addObject(new Block(),j,i);
            }
        }
        if(Greenfoot.isKeyDown("Right")&&move==true)
        {
            addObjects++;
        }
    }
    public void test()
    {
        addObject(new Help(),467,250);
    }
    public void test2()
    {
        List<Help> help=getObjects(Help.class);
        removeObjects(help);
    }
}
the move field is only supposed to b true if getX()>342 but for some reason its only true if the penguin is not on the ground and its > 342 (please dont comment on the ways i use to get my results unless it'll solve the issue at hand)
danpost danpost

2016/10/21

#
I believe the codes, lines 28 through 35, of the Pengu class is the culprit. If pengu is touching any object at its right (including ground at lower-right), 'scene.move' is set to 'false' regardless of the x-coordinate of pengu.
Nosson1459 Nosson1459

2016/10/21

#
yeah but i need that in case theres something in the way bec if move is false then it goes back to the penguin moving then the penguin "sees" somethings there and stops moving
danpost danpost

2016/10/21

#
Nosson1459 wrote...
yeah but i need that in case theres something in the way bec if move is false then it goes back to the penguin moving then the penguin "sees" somethings there and stops moving
I think you should dispense with the 'moving' field altogether and just use the current conditions to determine whether it can move or not at that moment. If you need to (so that you do not check the same thing multiple times during the same act cycle), you can set a local variable to the results of the check on current conditions; but, conditions can change from act to act, so it makes little sense to hold the state in a field maintaining the value of the state from act to act.
You need to login to post a reply.