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

2013/10/26

Untouchable for 2 seconds

ddvink ddvink

2013/10/26

#
After I hit an enemy I want do be untouchable (for running away i.e.). Somebody's got a clue how to do that? Here's my code so far
public void jumpBackWhenHit(int jumpHeight){
        // bepaal richting
        // jump back when hit
        Actor actor = getOneIntersectingObject(RibbonPig.class);

        if(actor != null){
            System.out.println("I've been hit");
            // get back a little in x-direction
            flickeringRogue();
            jump(jumpHeight);
        }
        // be untouchable for 2 seconds
    }
SPower SPower

2013/10/26

#
// at the top:
private long hitStart, hitTime = 2000; // 2000 millis = 2 secs

// change your method:
public void jumpBackWhenHit(int jumpHeight){  
        // bepaal richting  
        // jump back when hit  
        Actor actor = getOneIntersectingObject(RibbonPig.class);  
  
        if(actor != null){  
            System.out.println("I've been hit");  
            // get back a little in x-direction  
            flickeringRogue();  
            jump(jumpHeight);
            hitStart = System.currentTimeMillis(); // now
        }  
        // be untouchable for 2 seconds  
    }

// where you check the touches:
if (hitStart +hitTime < System.currentTimeMillis()) { // now you're not untouchable anymore.
and btw are you dutch? (me too, ik ook XD)
ddvink ddvink

2013/10/26

#
Yes, ik ben Dutch ;-) Bedankt man! ;-)
You need to login to post a reply.