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

2017/10/15

URGENT My Actor's Movement

Hellster Hellster

2017/10/15

#
My Shark's movement won't work in my game, it stutters from now and then. My Seacreature doesn't move at all and I don't know what is wrong with the code. If someone could help it will be very helpfull MY SHARK CODE
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Shark here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Shark extends Actor
{
    /**
     * Act - do whatever the Shark wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        moveAndTurn();
        eat();       
    }
    public void moveAndTurn()
    {
         if (Greenfoot.isKeyDown("a"))
        {
            turn(-3);
        }
        if (Greenfoot.isKeyDown("d"))
        {
            turn(3);
        }   
        if (Greenfoot.isKeyDown("w"))
        {
            move(3);
        }
        if (Greenfoot.isKeyDown("s"))
        {
            move(-3);
        }  
    }
    public void eat()
    {
        Actor SeaCreature;
        SeaCreature = getOneObjectAtOffset(0, 0, SeaCreature.class);
        if (SeaCreature != null)
        {
            World world;
            world = getWorld();
            world.removeObject(SeaCreature);
        }
    }
   
}

MY SeaCreature Code: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class SeaCreature here. * * @author (your name) * @version (a version number or a date) */ public class SeaCreature extends Actor { /** * Act - do whatever the SeaCreature wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void Act() { // Add your action code here. move(4); moveAround(); } public void moveAround() { move(4); turn(Greenfoot.getRandomNumber(90)); } }
Super_Hippo Super_Hippo

2017/10/15

#
Your SeaCreature's act method needs to have the name "act", not "Act". What do you mean that it stutters? What exactly happens?
Hellster Hellster

2017/10/15

#
Super_Hippo wrote...
Your SeaCreature's act method needs to have the name "act", not "Act". What do you mean that it stutters? What exactly happens?
It works for five minutes and then stops works again after a minute then stops again and so on. Do you know why this happens
danpost danpost

2017/10/15

#
It is not apparent what might cause the stuttering. The only thing I see that you might try (although I kind of doubt it will help) is changing the variable name 'SeaCreature' in the 'eat' method to 'seaCreature' so as not to cause any confusion between the Class and the actor. The changes would be the first occurrences on lines 41, 42, 43 and 47.
Hellster Hellster

2017/10/15

#
danpost wrote...
It is not apparent what might cause the stuttering. The only thing I see that you might try (although I kind of doubt it will help) is changing the variable name 'SeaCreature' in the 'eat' method to 'seaCreature' so as not to cause any confusion between the Class and the actor. The changes would be the first occurrences on lines 41, 42, 43 and 47.
It still keeps stoping. See i press a w s and d to make the shark move but it doesn't move.
danpost danpost

2017/10/15

#
Hellster wrote...
It still keeps stoping. See i press a w s and d to make the shark move but it doesn't move.
Please post your current Shark class code for review.
Hellster Hellster

2017/10/15

#
danpost wrote...
Hellster wrote...
It still keeps stoping. See i press a w s and d to make the shark move but it doesn't move.
Please post your current Shark class code for review.
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Shark here. * * @author (your name) * @version (a version number or a date) */ public class Shark extends Actor { /** * Act - do whatever the Shark wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { moveAndTurn(); eat(); } public void moveAndTurn() { if (Greenfoot.isKeyDown("a")) { turn(-3); } if (Greenfoot.isKeyDown("d")) { turn(3); } if (Greenfoot.isKeyDown("w")) { move(3); } if (Greenfoot.isKeyDown("s")) { move(-3); } } public void eat() { Actor seaCreature; seaCreature = getOneObjectAtOffset(0, 0, seaCreature.class); if (seaCreature != null) { World world; world = getWorld(); world.removeObject(seaCreature); } } }
danpost danpost

2017/10/15

#
Hellster wrote...
< Code Omitted >
Before trying something, please post your World subclass code for review.
Hellster Hellster

2017/10/15

#
danpost wrote...
Hellster wrote...
< Code Omitted >
Before trying something, please post your World subclass code for review.
mport 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 OceanWorld extends World { private Shark shark; private seaCreature seac; /** * Constructor for objects of class MyWorld. * */ public OceanWorld() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(1000,800, 1); shark = new Shark(); addObject(shark, getWidth()/2, getHeight()/2); seac = new seaCreature(); addObject(seac,Greenfoot.getRandomNumber(1000),Greenfoot.getRandomNumber(800)); seac = new seaCreature(); addObject(seac,Greenfoot.getRandomNumber(1000),Greenfoot.getRandomNumber(800)); seac = new seaCreature(); addObject(seac,Greenfoot.getRandomNumber(1000),Greenfoot.getRandomNumber(800)); seac = new seaCreature(); addObject(seac,Greenfoot.getRandomNumber(1000),Greenfoot.getRandomNumber(800)); seac = new seaCreature(); addObject(seac,Greenfoot.getRandomNumber(1000),Greenfoot.getRandomNumber(800)); } }
danpost danpost

2017/10/15

#
Oh, wait. You were not supposed to change the name of the SeaCreature class itself -- just the variable name that points to a SeaCreature object:
public void eat()
{
    Actor seaCreature;
    seaCreature = getOneObjectAtOffset(0, 0, SeaCreature.class);
    if (seaCreature != null)
    {
        World world;
        world = getWorld();
        world.removeObject(seaCreature);
    }
}
Hellster Hellster

2017/10/15

#
danpost wrote...
Oh, wait. You were not supposed to change the name of the SeaCreature class itself -- just the variable name that points to a SeaCreature object:
public void eat()
{
    Actor seaCreature;
    seaCreature = getOneObjectAtOffset(0, 0, SeaCreature.class);
    if (seaCreature != null)
    {
        World world;
        world = getWorld();
        world.removeObject(seaCreature);
    }
}
Still doesn't work
danpost danpost

2017/10/15

#
I see you uploaded the scenario and the movement of the shark seems to be working just fine.
Hellster Hellster

2017/10/16

#
danpost wrote...
I see you uploaded the scenario and the movement of the shark seems to be working just fine.
yeh i fixed i am going to upload to new version
You need to login to post a reply.