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

2012/4/20

One object eating then recreating it in a different position.

1
2
lgbrf lgbrf

2012/4/20

#
I am looking to have one object (A) 'eat' another object (B) and then have it reset to a position of my choice, say 20,20. How can i do this? I've heard of something called getIntersectingObject, but not sure how to use it or if it is even relevant. thanks, help is much appreciated
danpost danpost

2012/4/20

#
What happens when it gets 'eaten' at the new location, say 20,20? Does it reappear back where it was originally or to a location it has not been yet? I can think of ways to do what you want (as far as you have explained), but it may not actually be what you want. More information is required.
lgbrf lgbrf

2012/4/20

#
i dont think i explained clearly, B eats A, and A 'loses a life' and is reset back to 20,20
lgbrf lgbrf

2012/4/20

#
i dont think i explained clearly, B eats A, and A 'loses a life' and is reset back to 20,20
lgbrf lgbrf

2012/4/20

#
if what your saying is what happens if it is eaten instantly, that cant happen
danpost danpost

2012/4/20

#
I am going to assume that there is one, and no more than one, instance of 'A' class objects in the world at any time (but can be none, if 'eaten'). In the world class act() method, add
if (getObjects(A.class).isEmpty())
{
    lifeCounter.subtract(1);
    if (lifeCounter.getValue() == 0) gameOver(); else addObject(new A(), 20, 20);
}
This code will have to be adjusted for your scenario, but is the basics of what you need.
lgbrf lgbrf

2012/4/20

#
how does this make it B 'eat' A if they are intersecting, I dont rly understand this?
danpost danpost

2012/4/20

#
Soooo sorry. For some reason, I was thinking you had sample code for that. In 'B' class act():
getWorld().removeObjects(getIntersectingObjects(A.class));
'getIntersectingObjects(Class cls)' returns a list of all cls objects that 'this' intersects; 'removeObjects(List list)' will not fail if the list is empty; SO, this one-liner should suffice (with the code provided above to re-insert the A.class object back into the world).
lgbrf lgbrf

2012/4/20

#
ohh ok i get it now! but im having one problem, i changed most of it from pseudocode, but im having problem with .isEmpty
danpost danpost

2012/4/21

#
What 'pseudo-code' do you have and where are you with the change? (post what code you do have). 'isEmpty()' is a method in the List class (you do not have to create one). 'getObjects(A.class)' returns a List of all A.class objects IN the world. 'isEmpty()' will return true if the List has no items in it. BTW: the code I supplied is not in pseudo-code form; that is to say, if you had a Counter class and a Counter object called 'lifeCounter' and methods in the Counter class called 'public void subtract(int dropAmt)' and 'public int getValue()', and you had a method in the world class called 'gameOver()', then what I supplied would be the actual code. Of course, your objects classes would have to be named 'public class A extends Actor' and 'public class B extends Actor'.
lgbrf lgbrf

2012/4/21

#
i added a few things:
  private void gameOver()
    {
        System.out.println("GAME OVER");
    }

    private void killPlayer(){
        if (getObjects(Player.class))  
        {  
            lifeCounter=lifeCounter - 1;  
            if (lifeCounter() == 0) gameOver(); else addObject(new Player(), 20, 20);  
        }  
    }



invader.class-

 getWorld().removeObjects(getIntersectingObjects(Player.class));  
    }
lgbrf lgbrf

2012/4/21

#
i didnt think it was pseudo code, but isEmpty() didnt work
danpost danpost

2012/4/21

#
You might want a 'Greenfoot.stop();' statement in the gameOver() method, and I assume that is in the world class; I assume that you are calling killPlayer() from the world class act() method, and the killPlayer() method is in the world class; Line 9 shows lifeCounter as an integer type variable, where in line 10, it looks like a method call. I assume line 18 (in the nvader.class) is in the act() method of that class or in a method that is called from the act() method of that class. Line 7 should be 'if (getObjects(Player.class).isEmpty())' (it should work as I just tested it with no problems)
lgbrf lgbrf

2012/4/21

#
i've fixed all this up with no sytnax errors, but it just disappears and doesnt re appear :/. Invader code:
 getWorld().removeObjects(getIntersectingObjects(Player.class)); 
World code:
 private void gameOver()
    {
        System.out.println("GAME OVER");
        Greenfoot.stop();
    }

    private void killPlayer(int lifeCounter){
        if (getObjects(Player.class).isEmpty()); 
        {  
            lifeCounter=lifeCounter - 1;  
            if (lifeCounter == 0) gameOver(); else addObject(new Player(), 250, 510);  
        }  
    }
danpost danpost

2012/4/21

#
Is lifeCounter initialized to a value of more than one?
There are more replies on the next page.
1
2