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

2017/4/4

getX not working.

Alex12 Alex12

2017/4/4

#
Hey guys, got a problem with this code.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;
import java.awt.Color;
import java.awt.Font;
/**
 * Write a description of class andreas here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class andreas extends Actor
{
    /**
     * Act - do whatever the andreas wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        if(this != null && this.getWorld() != null)
        {

            setLocation(getX()-3,getY());
            checkposition1();
            checkTouching();

        }

    }    

    public void checkTouching()
    {
        if(this != null && this.getWorld() != null)
        {
            if(isTouching(explosion.class))
            {
                removeTouching(explosion.class);
                MyWorld myWorld = (MyWorld)getWorld();
                myWorld.addScore(2);
                getWorld().addObject(new missile(), 35, 375);
                Greenfoot.playSound("sounds/andreas.mp3");
                getWorld().removeObject(this);
                return;
            }
            if(isTouching(andreas.class))
            {

                getWorld().addObject(new andreas(),1550, Greenfoot.getRandomNumber(900));
                getWorld().removeObject(this);
                return;
            } 
        }
    }
    
    public void checkposition1()
    {
        if(getX() == 10)
        {
            getWorld().addObject(new TextField(1600, 200, Color.white, Color.black ,"       Click Reset for"), 800, 200);
            getWorld().addObject(new TextField(1600, 200, Color.white, Color.black ,"       "), 800, 0);

            getWorld().addObject(new TextField(1600, 200, Color.black, Color.blue ,"       GAME OVER"), 800, 400);
            getWorld().addObject(new TextField(1600, 200, Color.white, Color.black ,"       another shot."), 800, 600);
            getWorld().addObject(new TextField(1600, 200, Color.white, Color.black ,"   "), 800, 800);
            Greenfoot.stop();
        }
    }

}
Its about the checkposition1() method. It is simply not working, I dont even get an error. No idea whats the problem. I already tried the code in a different greenfoot project, still no response.
danpost danpost

2017/4/4

#
Alex12 wrote...
the checkposition1() method ... is simply not working, I dont even get an error. No idea whats the problem. I already tried the code in a different greenfoot project, still no response.
At what position in the world are you adding the andreas object?
Alex12 Alex12

2017/4/4

#
Random somewhere at the right border.
danpost danpost

2017/4/4

#
Alex12 wrote...
Random somewhere at the right border.
How wide is your world? and show the statement where you add the andreas object into the world.
Alex12 Alex12

2017/4/4

#
1600x900px
public void act()
    {
        if (Greenfoot.getRandomNumber(150) < 1)
        {
            addObject(new andreas(), 1550, Greenfoot.getRandomNumber(900));
        }
        if (Greenfoot.getRandomNumber(150) < 1)
        {
            addObject(new davis(), 1550, Greenfoot.getRandomNumber(900));
        }
        startingmessage();
    }
danpost danpost

2017/4/4

#
Okay 1550, where you place it, minus 10, where you check for it at, is 1540 even. Now, 1540, the difference between the two, divided by 3, the speed of the andreas object, is 513 with a remainder of one. That means its location around 10 will be at 11 and 8. It will never be exactly at 10 where you are checking for it to be at.
Alex12 Alex12

2017/4/4

#
Thank you very much. Never considered it could hang together with the speed. Thanks :) Could i try to check for it to be under f.e. 15?
danpost danpost

2017/4/4

#
Alex12 wrote...
Thank you very much. Never considered it could hang together with the speed. Thanks :) Could i try to check for it to be under f.e. 15?
You could check it to under any value; but to be equal to a value, you need to be more careful. You could use 2, 5, 8, 11, 14, 17, or 20.
You need to login to post a reply.