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

2021/12/21

need help with coding my movement using keys

roonie01 roonie01

2021/12/21

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

/**
 * Write a description of class MyWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class MyWorld extends World
{
    int x;
    int y;
    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public MyWorld()
    {    
        // Create a new world with 1600x1200 cells with a cell size of 1x1 pixels.
        super(1600, 1200, 1); 
         
        x = getWidth()/2; 
        y = getHeight()/2;
        
        
        //adds the image for the background
        GreenfootImage background = new GreenfootImage("space.png");

        // makes a new instance for rocket
        Rocket rocket = new Rocket ();
        addObject(rocket,x,y); 

        
    }

    public void movements()
    {
        if(Greenfoot.isKeyDown("w"))
        {


        }
        if(Greenfoot.isKeyDown("s"))
        {

        }
        if(Greenfoot.isKeyDown("a"))
        {

        }
        if(Greenfoot.isKeyDown("d"))
        {

        }
    }
}
danpost danpost

2021/12/21

#
The world does not "move". All codes above between lines 36 through 55 should be in some Actor subclass. Also, unless the coordinate of the center of the world are to be used move often, there is no need for them to be retained in fields. The following is sufficient:
import greenfoot.*;

public class MyWorld extends World
{
    public MyWorld()
    {
        super(1600, 1200, 1);
        addObject(new Rocket(), getWidth()/2, getHeight()/2);
    }
}
roonie01 roonie01

2021/12/21

#
so i moved my movements to the actor that the movements is called for this is what my code looks like but my actor still don't move and neither the world or actor have a image
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class rocket here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */

public class Rocket extends Actor
{
    GreenfootImage rocket = new GreenfootImage("shipNoFire.png ");
    GreenfootImage ship = new GreenfootImage("ship.png ");
    int x;
    int y;
    /**
     * Act - do whatever the rocket wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        x = this.getX();
        y = this.getY(); 
        // initialize rocket class
        
       movements();
        

    }
    public void movements()
    {
        if(Greenfoot.isKeyDown("w")&& y <= 1200)
        {
            
               y = y ++ ;
        
        }
        if(Greenfoot.isKeyDown("s")&& y >= 0)
        {
               y = y --;
        }
        if(Greenfoot.isKeyDown("a")&& x <= 1600)
        {
               x = x --;
        }
        if(Greenfoot.isKeyDown("d")&& x >= 1600)
        {
               x = x ++;
        }
    }
    
    }



You need to login to post a reply.