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

2016/4/20

2 Actors will not have the same X-Position value

Decapitated Decapitated

2016/4/20

#
Everything works, even the "getX()", but for some reason the Sanic actor and the Pipe actor never have the same X (Btw, i'm making a flappy bird type game)
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class Sanic extends Actor
{
    private Counter counter;
    public Sanic(Counter pointCounter){
        counter = pointCounter;
    }
    public void act() 
    {
        gravity();
        addScore();
    }    
    public void gravity(){
        if(Greenfoot.mouseClicked(null)){
           for(int i = 0;i < 100;i++){
               setLocation(getX(), getY()-1);
               if(getRotation() < 270){
                   setRotation(getRotation() - 40);
               }
           }
        }else{
            setLocation(getX(), getY()+10);
            if(getRotation() > 90 || getRotation() < 90){
                setRotation(getRotation()+5);
            }
        }
    }
    public void addScore(){
        Actor pipeTop = (Actor)getWorld().getObjects(Pipe_Top.class).get(0);
        Actor pipeBottom = (Actor)getWorld().getObjects(Pipe_Bottom.class).get(0);
        if(getX() < pipeTop.getX() && getX() > (pipeTop.getX()-4) && getOneObjectAtOffset(0, 0, Pipe_Top.class) == null && getOneObjectAtOffset(0, 0, Pipe_Bottom.class) == null){
           counter.add(1);
        }
    }
}
danpost danpost

2016/4/20

#
In a flappy bird type scenario, the bird usually remains along the same x-coordinate. It might be better to have the score controlled by either the Pipe_Top or Pipe_Bottom objects. You could still use the Sanic object's location within that class for the scoring. You could use a boolean field to flag whether the object has created a score yet or not.
Decapitated Decapitated

2016/4/20

#
I think I know why it never has the same X, because of how many pixels left i move the pipes. For example say the pipe starts at 10 and it moves left 3 at a time, and the "bird" is at 2 forever. The pipe will never be at 2 because: 10,7,4,1. Thanks for trying to take the time to help though
danpost danpost

2016/4/20

#
Decapitated wrote...
I think I know why it never has the same X, because of how many pixels left i move the pipes. For example say the pipe starts at 10 and it moves left 3 at a time, and the "bird" is at 2 forever. The pipe will never be at 2 because: 10,7,4,1. Thanks for trying to take the time to help though
I did not try to explain that all out because I felt that it would be much easier to do what I suggested. I only forgot to mention that the check would be for "equal to or greater (or less) than".
Decapitated Decapitated

2016/4/20

#
Fix: I just had the "bird" on a certain X so as the pipe went
move(-10);
it would end up meeting. "Bird" code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class Sanic extends Actor
{
    private Counter counter;
    public Sanic(Counter pointCounter){
        counter = pointCounter;
    }
    public void act() 
    {
        gravity();
        addScore();
    }    
    public void gravity(){
        if(Greenfoot.mouseClicked(null)){
           for(int i = 0;i < 100;i++){
               setLocation(getX(), getY()-1);
               if(getRotation() < 270){
                   setRotation(getRotation() - 40);
               }
           }
        }else if(getOneObjectAtOffset(0, 60, Ground.class) != null){
            setLocation(getX(), getY());
        }else{
            setLocation(getX(), getY()+10);
            if(getRotation() > 90 || getRotation() < 90){
                setRotation(getRotation()+5);
            }
        }
    }
    public void addScore(){
        Actor pipeTop = (Actor)getWorld().getObjects(Pipe_Top.class).get(0);
        Actor pipeBottom = (Actor)getWorld().getObjects(Pipe_Bottom.class).get(0);
        if(getX() == pipeTop.getX() && getOneObjectAtOffset(0, 0, Pipe_Top.class) == null && getOneObjectAtOffset(0, 0, Pipe_Bottom.class) == null){
           counter.add(1);
        }
    }
}
Pipe code(2 pipes in sub classes of the Pipe class ):
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class Pipe extends Actor
{
    public void act() 
    {
    }    
    public void pipeMovement(String pipe){
        Actor pipeTop = (Actor)getWorld().getObjects(Pipe_Top.class).get(0);
        Actor pipeBottom = (Actor)getWorld().getObjects(Pipe_Bottom.class).get(0);
        Actor pipeTopObj = new Pipe_Top();
        Actor pipeBottomObj = new Pipe_Bottom();
        int randomPipe = Greenfoot.getRandomNumber(100);
        if(isAtEdge() && getX() < 50){
            pipeTop.setLocation(1280, randomPipe);
            pipeBottom.setLocation(pipeTop.getX(), pipeTop.getY()+620);
        }else{
            if(pipe == "top"){
                move(-10);
            }else if(pipe == "bottom"){
                setLocation(pipeTop.getX(), getY());
            }
        }
    }
    public void spawnPipe(){
        Actor pipeTop = (Actor)getWorld().getObjects(Pipe_Top.class).get(0);
        Actor pipeBottom = (Actor)getWorld().getObjects(Pipe_Bottom.class).get(0);
        Actor pipeTopObj = new Pipe_Top();
        Actor pipeBottomObj = new Pipe_Bottom();
        int randomPipe = Greenfoot.getRandomNumber(100);
        if(pipeTop.getX() < 960 ){
            getWorld().addObject(pipeTopObj, 1280, randomPipe);
            getWorld().addObject(pipeBottomObj, 1280, randomPipe+620);
        }
    }
}
I don't know why I didn't think of this before. Hope I can help some other confused chump like me :P
You need to login to post a reply.