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

2014/8/28

Parameter pandemic

BackdoorHermet BackdoorHermet

2014/8/28

#
Trying to pass a parameter from actor A, to the world, to actor B. Actor A's act method runs this:
if(getWorld() instanceof levelOne)
        {
            levelOne currentLevel = (levelOne)getWorld();
            currentLevel.updateNinjaPosition(getX(), getY());
        }
The world receives it:
public void updateNinjaPosition(int x, int y)
    {
        ninjaX = x;
        ninjaY = y;
    }
The world sends it:
public void updateFollowers()
    {
        sword.ninjaInfo(ninjaX, ninjaY);
        tie.ninjaInfo(ninjaX, ninjaY);
        
        if(enemyCount >= 1)
        {
                enemy.getInfo(ninjaX, ninjaY);  
        }
    }
The actor receives it:
public void getInfo(int x, int y)
    {
        ninjaX = x;
        ninjaY = y;
    }
The thing that isn't working though, is actor A's position is correct when it is sent to the world for tie and sword, but NOT for enemy. I'm not sure why, but setting the condition "if(enemyCount >= 1)" seems to disrupt the flow and it doesn't work. Any thoughts?
NikZ NikZ

2014/8/28

#
Do you call updateFollowers()?
danpost danpost

2014/8/28

#
How do you control the value of 'enemyCount'? Show all code pertaining to this field stating where each part is located.
BackdoorHermet BackdoorHermet

2014/8/28

#
public class levelOne extends SWorld
{
    int ninjaX = 0;
    int ninjaY = 0;
    int ninjaDirection = 0;
    
    int enemyTimer = 0;
    int enemySpawnTimer = Greenfoot.getRandomNumber(300)+50;
    int enemies = 15; //Enemies per level
    int enemyCount = 0;
    
    int myPoints = 0;
    int t = 0;
    
    tie tie;
    Sword sword;
    Ninja ninja;
    Enemy enemy;
    smallText points; 
    /**
     * Constructor for objects of class levelOne.
     * 
     */
    public levelOne()
    {    
        super(960, 400, 1, 2000); 
        
        ninja = new Ninja();
        setMainActor(ninja, 480, 200);
        
        tie = new tie();
        addObject(tie, ninja.getX()-35, ninja.getY()-22);
        
        sword = new Sword();
        addObject(sword, ninja.getX(), ninja.getY());
        sword.setRotation(-45);
        
        GreenfootImage bg = new GreenfootImage("cityBackground2.gif");
        //setScrollingBackground(bg);
        
        backdrop backdrop;
        backdrop = new backdrop();
        addObject(backdrop, 480, 200);
        
        sky sky;
        sky = new sky();
        addObject(sky, 480, 200, false);

        setPaintOrder(Ninja.class, ninjaStar.class, Sword.class, tie.class, Blood.class, Enemies.class, GameText.class, backdrop.class, sky.class);
        
        populateText();
    }
    
    public void act()
    {
        super.act();
        updateFollowers();
        enemyTimer++;

        if(enemies != 0)
            if(enemyTimer == enemySpawnTimer)
                populateEnemy();
    }
    
    public void updateFollowers()
    {
        sword.ninjaInfo(ninjaX, ninjaY);
        tie.ninjaInfo(ninjaX, ninjaY);
        
        if(enemyCount >= 1)
        {
                enemy.getInfo(ninjaX, ninjaY);
                System.out.println("l1: " + ninjaX);
           
        }
    }
    
    public void populateText()
    {
        points = new smallText("Score: " + myPoints);
        addObject(points, 155, 30, false);
    }
    
    public void populateEnemy()
    {
        int x = Greenfoot.getRandomNumber(2);
        int X = 0;
        int Y = Greenfoot.getRandomNumber(300)+50;

        enemy = new Enemy(x);
          
        if(x == 1)
            X = -40;
        else
            X = 1000;
            
        addObject(new Enemy(x), X, Y);
        enemies--;
        enemyCount++;
        enemyTimer = 0;
    }
    
    public void enemyCount()
    {
        enemyCount--;
    }
BackdoorHermet BackdoorHermet

2014/8/28

#
 public void updatePoints(int p)
    {
        myPoints = myPoints + p;
        points.setGameText("Score: " + myPoints);
    }
    
    public void updateNinjaPosition(int x, int y)
    {
        ninjaX = x;
        ninjaY = y;
    }
    
    public void updateNinjaDirection(int nDirection)
    {
        ninjaDirection = nDirection;
        points.setGameText("Score: " + myPoints);
    }
}
danpost danpost

2014/8/29

#
First, you have a reference to the ninja in this 'levelOne' world. There is no need to have the ninja pass its x and y to the world -- 'ninja.getX()' and 'ninja.getY()' can be used straight away (getting the most current position of the actor). You can eliminate the 'ninjaX' and 'ninjaY' fields from the class as well as the 'updateNinjaPosition' method. You may be able to do similar with the 'ninjaDirection' field (depending on exactly what it refers to). What you could use is a method in the world class to pass the ninja actor itself:
public Ninja getNinja()
{
    return ninja;
}
so your actors (sword, tie, and enemy) can call this method instead of passing the info to them when they are not ready (you can then remove from the 'levelOne' world class the 'updateFollowers' method). Put the following in the 'Sword', 'tie', and 'Enemy' classes:
// instance field
private Ninja ninja;
// with this method to set it
[code]public void addedToWorld(World world)
{
    if (world instanceof levelOne) ninja = ((levelOne)world).getNinja();
    // reserved for different levels
}
With this, all your actors that follow the ninja will keep their own reference to it. No need to pass anything else. Just make sure the ninja is in the world before trying to get the x and y:
if (ninja.getWorld() == null) return;
will exit the method if the ninja is not in the world.
You need to login to post a reply.