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

2012/3/3

Help!

1
2
3
Dom Dom

2012/3/3

#
Hi- I'm new to Greenfoot and I wanted to ask you nice people two things. I've started creating a game, and I need to know how to: 1) Make enemies move right and left automatically, on top of a single platform. 2) What coding I need to make the actor/hero die if he touches any of the enemies.
Duta Duta

2012/3/3

#
Here's some code snippets:
//Put this in your field declaration section
int speed = 3;

//This code moves an actor right
setLocation(getX() + speed, getY());

//This code moves an actor left
setLocation(getX() - speed, getY());

//This code removes an actor if it is touching an enemy
if(getOneIntersectingObject(Enemy.class) != null)
	getWorld().removeObject(this);
Dom Dom

2012/3/3

#
Thanks alot, but the move right left thing didnt work, this is what i did- }
public class Saibaman extends Actor
{
    /**
     * Act - do whatever the Saibaman wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    
    int speed = 3;
    
    public void act() 
    {
    setLocation (getX() + speed, getY());
    setLocation (getX() - speed, getY());
    }
 }
danpost danpost

2012/3/3

#
It would probably be easiest to have the platform control the movement of the enemy on it. This would entail creating, in the Platform class, an instance variable of Enemy type for the enemy on it, and an instance variable of integer type for the current direction of the enemy on it:
private Enemy enemy = null;
private enemyDirection = 1;
a way (method) to tell the platform what enemy object is on it (in the Enemy class)
public void setEnemy(enemy)
{
    this.enemy = enemy;
}
and the control of the enemy in the act() method of the platform class:
if (enemy != null)
{
    boolean test1 = (enemy.getX() + 1 > getX() + getImage().getWidth() / 2 && enemyDirection == 1);
    boolean test2 = (enemy.getX() - 1 < getX() - getImage().getWidth() / 2 && enemyDirection == -1);
    if (test1 || test2) // if a either end of platform
    { // turn around
        enemyDirection = -enemyDirection;
        GreenfootImage img = enemy.getImage();
        img.mirrorVertically();
        enemy.setImage(img);
    }
    enemy.setLocation(enemy.getX() + enemyDirection, enemy.getY());
}
Dom Dom

2012/3/3

#
Where you've written enemy, should I replace it with the enemy class name?
Duta Duta

2012/3/3

#
Dom wrote...
Thanks alot, but the move right left thing didnt work, this is what i did- }
public class Saibaman extends Actor
{
    /**
     * Act - do whatever the Saibaman wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    
    int speed = 3;
    
    public void act() 
    {
    setLocation (getX() + speed, getY());
    setLocation (getX() - speed, getY());
    }
 }
Although danpost posted a good code section, I thought I'd just point out the problem with this - what's happening is when act() is executed, the actor moves right by <speed> amount, then moves left by exactly the same amount, meaning it appears to not move at all.
danpost danpost

2012/3/3

#
The only enemy you should have to replace with your enemy class name is the one starting with the uppercase letter in
private Enemy enemy = null;
OH. and I missed this the first line in the setEnemy method should be
public void setEnemy(Enemy enemy)
again replacing 'Enemy' with your enemy class name.
Dom Dom

2012/3/3

#
It says here '<identifier> expected'
private enemyDirection = 1;
danpost danpost

2012/3/3

#
Sorry, should be
private int enemyDirection = 1;
Dom Dom

2012/3/3

#
THIS IS SO FRUSTRATING- Its now saying for:
public class Saibaman extends Actor
{
    /**
     * Act - do whatever the Saibaman wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
   public void setEnemy(Saibaman enemy)
   {
    this.enemy = enemy;
   }
} 
'cannot find symbol - variable enemy'
danpost danpost

2012/3/3

#
Sorry, my mistake, again. That method should be in the platform class with the other code (not in the Enemy class, as I erroneously stated above).
Dom Dom

2012/3/3

#
Looks like this is not working. I get problems no matter what i do. Is there no simpler way to merely making an enemy move 100 pixels to the right of the platform and then stopping, turning and moving left to the other end, and repeat this process?
danpost danpost

2012/3/3

#
Is the code provided not working, or are you not getting the results you want? Specify the nature of the current problem. You could work it the other way; that is, have the enemy control its own movements; but, it would have to check and make sure it is still on the platform each step of the way.
danpost danpost

2012/3/3

#
With the code I provided above, did you, in your world class, when adding an enemy on a platform, inform the platform that the enemy was there with
Platform platform = new Platform();
addObject(platform, 200, 200); // (200, 200) change to wherever
Saibaman saibOne = new Saibaman;
addObject(saibOne, 200, 185); // (200, 185) change to wherever (on platform)
platform.setEnemy(saibOne); // informs platform of enemy upon it
Dom Dom

2012/3/3

#
The code provided isn't working, and I would prefer that other way you just mentioned. If you could just post the coding i need for that? Thanks a lot, Danpost. Your help is really appreciated.
There are more replies on the next page.
1
2
3