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

2013/10/28

Untouchable for two seconds

ddvink ddvink

2013/10/28

#
Hi there, I don't have any code near me, so I cant place that, but... A Char i am controlling for a game, has to be untouchable for 2 seconds for the mobs in the game. To get the char in touch for that class, I use something like the getObjectAtIntersection method. That's working well. That method is called in the act method. That works fine, but how can I dissable that for just two seconds? Yours, Dennis
danpost danpost

2013/10/28

#
Add an instance field, an 'int noTouchTimer' and add the following methods to the class:
private void runNoTouchTimer()
{
    if (noTouchTimer == 0) return;
    noTouchTimer--;
}

private void startNoTouchTimer()
{
    noTouchTimer = 100;
}

private boolean canMobTouch()
{
    return noTouchTimer == 0;
}
Call the 'runNoTouchTimer' method from the 'act' method. Call the 'startNoTouchTimer' method to disable mobs touching for about 2 seconds. Use 'canMobTouch' to place condition on touch checking.
ddvink ddvink

2013/10/28

#
Thanx Call the 'startNoTouchTimer' method to disable mobs touching for about 2 seconds. Is there an easy way to realise this? (the 2 seconds thing) and what stands the 100 for in this case? Yours, Dennis
ddvink ddvink

2013/10/28

#
There's a bee and three ants on my screen... This is my code so far import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Bee here. * * @author (your name) * @version (a version number or a date) */ public class Bee extends Actor { private int noTouchTimer; public Bee(){ } public void act() { checkKeys(); runNoTouchTimer(); checkAnt(); } public void flickeringTwoSeconds(){ } public void checkAnt(){ Actor actor = getOneIntersectingObject(Ant.class); if (actor != null){ System.out.println("Een mier wordt aangeraakt!"); startNoTouchTimer(); } } public void checkKeys(){ if(Greenfoot.isKeyDown("down")){ setLocation(getX(), getY()+3); } if(Greenfoot.isKeyDown("up")){ setLocation(getX(), getY()-3); } if(Greenfoot.isKeyDown("left")){ setLocation(getX()-3, getY()); } if(Greenfoot.isKeyDown("right")){ setLocation(getX()+3, getY()); } } public void runNoTouchTimer(){ if(noTouchTimer == 0) return; noTouchTimer--; } public void startNoTouchTimer(){ System.out.println("Start no touch wordt aangeroepen!"); noTouchTimer = 100; } public boolean canMobTouch(){ return noTouchTimer == 0; } }
danpost danpost

2013/10/28

#
Scenarios running at normal running speeds run between 50 and 60 frames per second. 50 * 2 = 100. So, we are counting frames (or act cycles) for about 2 seconds.
ddvink ddvink

2013/10/28

#
Where do I have to put this? Use 'canMobTouch' to place condition on touch checking. Thanks in advance, Dennis
danpost danpost

2013/10/28

#
ddvink wrote...
Where do I have to put this? Use 'canMobTouch' to place condition on touch checking. Thanks in advance, Dennis
In the class of the Char you are controlling.
You need to login to post a reply.