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

2012/4/4

removing objects at the left of screen

1
2
3
4
e_e13 e_e13

2012/4/4

#
Hello all, How do I change this code so that the objects will be removed when they hit the left edge of the screen (rather then the right)?
public void act() 
    {
if (this.atWorldEdge()==true)
        {
           World world;
           world = getWorld();
           world.removeObject(this);
           return;
        }
    
    }  
   
    public boolean atWorldEdge()
    {
        if (getX() > getWorld().getWidth() - getImage().getWidth() || getY() > getWorld().getHeight() - getImage().getHeight())    
    
        {    
            return true;    
        }    
        else    
        {    
            return false;
        }    
    }
trash1000 trash1000

2012/4/4

#
public boolean atWorldEdge() {
	// check for right edge
	if(getX() > getWorld().getWidth()-getImage().getWidth()) {
		return true;
	}
	// check for left edge
	if(getX() < getImage().getWidth()) {
		return true;
	}
	// check for bottom edge
	if(getY() > getWorld().getHeight()-getImage.getHeight()) {
		return true;
	}
	// check for top edge
	if(getY() < getHeight()) {
		return true;
	}
	else {
		return false;
	}
}
Like this. You are at the left edge when the x-coordinate is 0 or less than your object's width. /e: However, this should work but I didn't test it.
SPower SPower

2012/4/4

#
public void act()   
{  
     if (atWorldEdge() == true)  
        {  
           getWorld().removeObject(this);  
        }  
}    
     
public boolean atWorldEdge()  
{ 
     if (getX() == 0) {
            return true;
     } else {
            return false;
     }
}
This will remove the object if it is at the left side of the world. If you also want it to be deleted when it's on the right side, change if(getX() == 0) to if(getX() == (0 || getWorld().getWidth() )
SPower SPower

2012/4/4

#
Sorry, in atWorldEdge() you have to do this if you want the object to also be deleted when it's at the right edge of the word:
if (getX() == (0 || getWorld().getWidth() -1) {
    return true;
} else {
    retrun false;
}
e_e13 e_e13

2012/4/4

#
thanks :) It worked perfectly
davmac davmac

2012/4/4

#
You are at the left edge when the x-coordinate is 0 or less than your object's width.
Not quite correct; you are at the left edge when the x-coordinate is less than half your object's width (since the coordinates specify the position of the center of the object).
SPower SPower

2012/4/4

#
davmac is right
e_e13 e_e13

2012/4/4

#
sorry to ask again (I don't know if I should rather just make a new discussion), but how do I transmit int's between classes? Now that I've triggered something I want to send an conformation of this to another class, and when a total number of 3 of these conformations have been sent to that class I can trigger something else. I've done a google for this, but as you can probably see I'm a bit lacking in terminology so I thought maybe someone here could help. I would provide code, but I simply don't even know where to start. Again, thanks for everyone's help :)
danpost danpost

2012/4/4

#
The code for what you want would vary depending on exactly what you want. If the world is what instantiates this new action when 3 confirmations are received you would code one thing. If an Actor class (but not a specific actor) receives them to do something, you would code something else. if a specific actor receives them and acts upon them, you would code some other thing.
trash1000 trash1000

2012/4/4

#
http://en.wikipedia.org/wiki/Object_composition#Aggregation I think this is what you want to do. You have a variable storing a reference to another object and use the variable to call a method of that object:
private Object knowsObject;

public Class(Object pObject) {
    knowsObject = pObject; // You now have a reference to an object (you could also set it with a setter method)
    knowsObject.method(); // Call whatever method (i.e. to send an integer) of that object 
}
davmac davmac

2012/4/4

#
Or, click the "Documentation" link and read through Tutorial #6.
SPower SPower

2012/4/5

#
Let's say you're going to send ints from obj 1 to obj 2. In obj 2, you must define an action which sets its integer to the parameter. That's it.
SPower SPower

2012/4/5

#
Sorry: I forgot something. In your subclass of world, you must have an instance variable of obj 2, and an action which returns it. Than you have to create an object of that world, and you're ready to go. If I didn't explain it so you can understand it, look at tutorial 6 in documentation, just like Davmac said.
e_e13 e_e13

2012/4/5

#
Thanks all for the replies, I'm sorry I had to ask since I didn't realise there was a tutorial on the site :/ It's working now :)
danpost danpost

2012/4/5

#
Other than the fact that you will create hundreds of these objects, once the 'totalCount' is bumped to 1, I do not see any problems here. To fix what I saw, change your act method to
public void act()
{
    World world;
    world = getWorld();
    if (totalCount == 1 && world.getObjects(GameWon.class).isEmpty())
    {
        GameWon gw;
        gw = new GameWon();
        world.addObject(gw, 400, 50);
    }
}
or just insert line 4 below
if (totalCount == 1)
{
    getWorld().addObject(new GameWon(), 400, 50);
    totalCount == 0; // prevents more GameWon object from being created
}
There are more replies on the next page.
1
2
3
4