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

#
Id much rather the coding is in the enemy class, as it would get too complicated for me if the coding and the methods were inside the platform class.
danpost danpost

2012/3/3

#
import greenfoot.*;

public class Saibaman extends Actor
{
    private int direction = 1;

    public Saibaman()
    {
        // constructor code, if any
    }

    public void act()
    {
        if (getObjectAtOffset(direction, getY() + getImage().getHeight() + 1, Platform.class) == null)
        {
            direction = -direction;
            GreenfootImage img = getImage();
            img.mirrorVertically();
            setImage(img);
        }
        setLocation(getX() + direction, getY());
    }
}
Dom Dom

2012/3/3

#
'cannot find symbol - method getObjectAtOffset(int,int, java.lang.Class<Platform>'
Dom Dom

2012/3/3

#
I think the method you meant was 'getOneObjectAtOffset' ? Anyway, after I fixed that, my enemies keep on turning upside down ,and then, upright again and keep on doing that, lol.
danpost danpost

2012/3/3

#
The last post needs adjusted (getObjectsAtOffset return true only if the x and y are equal to the returned objects x and y, which will not work for this case). Give me a few to work with it, and I will get back. And mirrorVertically() should be mirrorHorizontally().
danpost danpost

2012/3/3

#
This will do what you want:
Dom Dom

2012/3/3

#
The code compiles, but, nope, still not doing what I want. Here's whats happening now with your code, and what I really want in detail. There are two enemies in this world. Both of these enemies are on different platforms. When I use your code, one of these enemies moves to the right of the platform, and stops when reaching the end, and the other simply turns right and left continuously in the same spot. What I want is for each enemy to move right and move to the end of the platform, and then move left and move to the left of the platform, and so on, continuing the process until the hero actor kills them or they kill the hero actor. The part of the hero killing them and them disappearing, or the hero touching them and dying has already been sorted. Again, thanks for your help, but just fix this thing for me.
danpost danpost

2012/3/3

#
Can you post the code for creating the platforms and enemies and related?
Dom Dom

2012/3/3

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

/**
 * Write a description of class Platform here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Platform extends Actor
{
    /**
     * Act - do whatever the Platform wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    private Saibaman enemy = null;
    private int enemyDirection = 1;
    
    
    public void act()
    {
        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());
    }
}
    public void setEnemy(Saibaman enemy)
    {
        this.enemy = enemy;
    }

   
    
}
Enemy Code:
import greenfoot.*;

public class Saibaman extends Actor
{
    private int direction = 1;

    public Saibaman()
    {
        // constructor code, if any
    }

    public void act()
    {
        setLocation(getX() + direction * (1 + getImage().getWidth() / 2), getY());
        if (getOneIntersectingObject(Platform.class) == null)
        {
            direction = -direction;
            GreenfootImage img = getImage();
            img.mirrorHorizontally();
            setImage(img);
            setLocation(getX() + (2 + getImage().getWidth() / 2) * direction , getY());
        }
        else
        {
            setLocation(getX() - direction * getImage().getWidth() / 2, getY());
        }
    }
}
danpost danpost

2012/3/3

#
Change your Platform class code back to:
import greenfoot.*;

public class Platform extends Actor
{
}
and if you still have issues with the one turning left and right in place, see if increasing its Y location in the world helps.
Dom Dom

2012/3/3

#
Same thing happens. One enemy faces left, faces right,faces left faces right and never gets tired, whilst the other enemy goes all the way to the end of the platform on the right and then just stops. Its like both of the enemies are doing the right method, but one of the enemies is doing part of it and the other enemy is doing the other part of the method, if that makes sense. they should both be doing the whole method. Shall I just give up?
Dom Dom

2012/3/3

#
And I did try and change the Y location, it still didnt work.
danpost danpost

2012/3/3

#
Never give up! What does your world class look like?
Dom Dom

2012/3/3

#
Look at this game, 'Jumper Pelican', by Azuresky. What I want from my enemies is what the snake is doing in that game, but I don't know how to incorporate that code into my game. http://www.greenfoot.org/scenarios/526
Dom Dom

2012/3/3

#
My World Class:
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.
     * 
     */
    public MyWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 
        
        addObject ( new Goku    (),  19 , 348 ) ;
        addObject ( new Platform(),  49 , 385 ) ;
        addObject ( new Platform(), 149 , 385 ) ;
        addObject ( new Platform(), 349 , 385 ) ;
        addObject ( new Platform(), 449 , 385 ) ;
        addObject ( new Platform(), 549 , 385 ) ;
        addObject ( new Platform(), 142 , 147 ) ;
        addObject ( new Platform(), 349 , 254 ) ;
        addObject ( new Platform(), 554 , 135 ) ;
        addObject ( new Senzu   (), 584 , 103 ) ;
        addObject ( new Saibaman(), 107 , 115 ) ;
        addObject ( new Saibaman(), 518 , 103 ) ;

    }
}
There are more replies on the next page.
1
2
3