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

2016/10/16

Random line of code effects

Nosson1459 Nosson1459

2016/10/16

#
with the getOneObjectAtOffset method when the penguin (from pengu scenario) hits a block on top of it it doesn't and shouldn't go any higher cause it hit something (I reversed the vSpeed), but if I add a GreenfootImage variable into the block without even setting any images I just define the variable the penguin starts shaking up and down when it hits the block then starts jumping on top of the block
danpost danpost

2016/10/16

#
Nosson1459 wrote...
with the getOneObjectAtOffset method when the penguin (from pengu scenario) hits a block on top of it it doesn't and shouldn't go any higher cause it hit something (I reversed the vSpeed), but if I add a GreenfootImage variable into the block without even setting any images I just define the variable the penguin starts shaking up and down when it hits the block then starts jumping on top of the block
Please post your current Pengu class code so we can review it for any possible issues. Chances are, however, your condition required to reverse direction when contacting the block is too tight and another condition must also be true before reversing direction. At any rate, without knowing what you currently have, we cannot assist very well.
Nosson1459 Nosson1459

2016/10/16

#
(btw it doesnt always happen its sporadic)
Nosson1459 Nosson1459

2016/10/16

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Box here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Box extends Actor
{
    private GreenfootImage image1=new GreenfootImage("mystery.png");
    private GreenfootImage image2=new GreenfootImage("MysteryG.png");
    public Box()
    {
       setImage(image1);
    }
    /**
     * Act - do whatever the Box wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        //
    }
    
}
this has the set image but even if it doesn't it still goes crazy (sometimes)
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 , null);
        return under != null;
    }
    
    public boolean onGround()
    {
        Object under = getOneObjectAtOffset(0, getImage().getHeight()/2-8 , 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.*;

/**
 * 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() 
    {
        coins();
        checkKeys();        
        checkFall();
        
    }
    
    private void checkKeys()
    {
        if (Greenfoot.isKeyDown("left") )
        {
            setImage("pengu-left2.png");
            if(!touchingLeftSide()||!touchingLeftSide2()||!touchingLeftSide3())
               moveLeft();
        }
        if (Greenfoot.isKeyDown("right") )
        {
            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;
        }
    }
}

    
Nosson1459 Nosson1459

2016/10/16

#
now it sometimes happens whether or not the GreenfootImage code is there
danpost danpost

2016/10/16

#
It looks like you have modified the Mover class by adding some methods as well as some code (at least in one place). It would probably be best if you left the class alone and coded any other needed code in the Pengu class (like the 'gameEnd' and 'bump' methods). I am not really a fan of the way the Mover class was coded for collision checking. All collision checking can be accomplished using the 'getIntersectingObjects' method. A decent processing order is, with moving horizontally and moving vertically separated, (1) move (2) check collision (3) move (in the opposite direction)back off of colliders and adjust speed, if needed. For vertical movement, if collision occurs while moving downward (vertical speed is positive), set a 'boolean onGround' field to true; (set it to false before collision checking). This is so that the next act cycle movement code can use that value to determine whether jumping is allowed or not.
danpost danpost

2016/10/16

#
Nosson1459 wrote...
now it sometimes happens whether or not the GreenfootImage code is there
This has nothing to do with the issue. If the Box object has an image, the Pengu will respond to it as coded; if not, it will be ignored.
Nosson1459 Nosson1459

2016/10/16

#
getIntersectingObjects is very limited in da sense dat it works 4 all sides not just bottom top etc.
danpost danpost

2016/10/17

#
Nosson1459 wrote...
getIntersectingObjects is very limited in da sense dat it works 4 all sides not just bottom top etc.
Well, it is all inclusive, in that it works for every pixel of the image, if that is what you mean. But, I believe I am correct in saying that you do not want to be intersecting the block at all, So, it is limited to only check those three points for intersection.
Nosson1459 Nosson1459

2016/10/19

#
i only want it 2 b true if i hit it from a specific side not any place
danpost danpost

2016/10/19

#
Nosson1459 wrote...
i only want it 2 b true if i hit it from a specific side not any place
Yes, I know; but, you can determine that from the direction of movement of the actor -- not from which point is currently intersecting.
Nosson1459 Nosson1459

2016/10/19

#
never thought about it like dat
You need to login to post a reply.