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

2019/4/27

Help with actor movements

fvila02 fvila02

2019/4/27

#
Hi there, I've been working on something for awhile now and am having issues with some of the change in movement. At the moment my code looks like this:
import greenfoot.*;
    
    
    public class Enemy2 extends Actor
    {
        
        GifImage r = new GifImage ("Rock.gif");
        boolean isDown = false;
        private GifImage r2 = new GifImage ("Rock2.gif");
        boolean move1=false;
        public void act() 
        {
        
        
                
                
                notMoving();
                bounceLeft();
                bounceRight();
               
       
    }    
    
    public void notMoving()
    {
        if(move1==false)
        {
            move(-2);
        }
    }
    public void bounceLeft()
    {
        if(getX() == 110)
        {
            
            
            move1=true;
            move(2);
            getImage().mirrorHorizontally();
        }
    }
    public void bounceRight()
    {
        if(getX() == getWorld().getWidth()-25)
        {
            
            move(-2);
            move1=true;
            getImage().mirrorHorizontally();
        }
    }



VVV      Still working on these last 2      VVV


    public void bounceTop()
    {
        if(getY() == 0)
        {
            
        }
    }
    public void bounceBottom()
    {
        if(getY() == getWorld().getHeight()-1)
        {
            
        }
    }
}
All that this does is go up to the left border and turn then stop. I've tried a few things but nothing is working atm
danpost danpost

2019/4/27

#
At what coordinates is the actor being placed into the world?
fvila02 fvila02

2019/4/27

#
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
{

    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    
    Enemy e = new Enemy();
    Enemy2 r = new Enemy2();
    Hero h = new Hero();
    public MyWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(800, 740, 1); 
        addObject(h, 100,300);
        addObject(r,400, 400);
        addObject(e,200, 300);
    }
}
At the moment this is my World and Enemy2 is the one im working on
danpost danpost

2019/4/27

#
So, are you wanting the actor to continuously move back and forth between x-coordinate values of 100 to 715?
fvila02 fvila02

2019/4/27

#
Yes I am im also going to add it so that it bounces off the top and bottom barriers but that'll come later as i know how to do that part from a previous project.
danpost danpost

2019/4/27

#
Ask yourself: "What state(s) are required for the horizontal movement of my actor?". The answer is (1) speed; and (2) direction. Or, combined, it is called velocity. Next, you said the behavior (currently) was to have the actor go back and forth, left, right, left, right... Now the question is: "When is the actor to turn around?". The answer to this is: when some limiting x-coordinate value is reached (or overtaken) when the actor moves toward it. The second part of this condition is crucial for two reasons. It prevents your actor from "spinning" in place if place outside its normal limits and it makes the code so much more simple. To turn around, the only thing you would need to do is to change the direction field -- or, negate the velocity field. Okay, The actors behavior is to move and turn around when required. So:
private int xVelocity = 2;

public void act()
{
    // moving horizontally
    move(xVelocity);
    // turning around at side limits
    if ((xVelocity < 0 && getX() < 100) || (xVelocity > 0 && getX() > getWorld().getWidth()-25))
    {
        xVelocity *= -1;
    }
}
fvila02 fvila02

2019/4/27

#
Thank you so much that helped a lot i modified it a little bit and make it into 2 if statements and added some code and it works perfectly now! Thanks a lot for the help!
You need to login to post a reply.