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

2012/9/17

Multiple funtions at one time

1
2
3
jhadad jhadad

2012/9/17

#
Hi, I am making a game where you have to take a lighter and light tinder on fire to start a fire. So there is a pile of little actors that make up the tinder, then you click the lighter and pull it over to the tinder and press v to blow on it. The only proble is once you start holding v you can no longer move the lighter around to catch all the tinder on fire. I was really hoping someone could help me out with this. Thanks alot John
danpost danpost

2012/9/17

#
Chances are you created a loop for holding down v. Instead, you need an instance boolean field to track if you have found the v key down or not.
// instance field
boolean vDown = false;
// within act or a method it calls (lightTinder(), or something like that)
if (!vDown && Greenfoot.isKeyDown("v")) vDown = true;
if (vDown && !Greenfoot.isKeyDown("v")) vDown = false;
if (vDown)
{
    // do what you need to do (catch tinder on fire)
}
This way you will still cycle through 'act's, allowing you to capture movement, while doing what you need to do. Actually, I do not think you need the boolean field at all. Just using 'if (Greenfoot.isKeyDown("v"))' should do the job. What code were you trying to use? ? 'while'?
jhadad jhadad

2012/9/17

#
I just tried using this method but its still freezing the mouse until after i release v
danpost danpost

2012/9/17

#
What method? Can you post the code in question? Make sure you give all the code neccessary to see the full picture of what is happening.
jhadad jhadad

2012/9/17

#
I meant method as in your way of doing the keyDown. So what i have is an actor lighter, and an actor tinder. using your way i have this code in my lighter class all in public void act() boolean vDown = false; if (!vDown && Greenfoot.isKeyDown("v")) vDown = true; if (vDown && !Greenfoot.isKeyDown("v")) vDown = false; if (vDown && getOneIntersectingObject(tinder.class)) { tinder t = (tinder); int xSpot = t.getX(); int ySpot = t.getY(); getWorld().removeObject(t); getWorld().addObject(new burningTinder(), xSpot, ySpot); }
jhadad jhadad

2012/9/17

#
Nevermind, i typed that in wrong because i just changed up my code to use the boolean as opposed to just isKeyDown, so that code is wrong. But I am also having trouble with that part. What i am trying to do it that when the lighter is touching the tinder, that tinder starts to burn, which is why i need to be able to move the lighter so you can light the entire pile of tinder
jhadad jhadad

2012/9/17

#
and the lighter is just set up to follow the mouse around the screen
jhadad jhadad

2012/9/17

#
I just saw your edit and that is what i was using initially
danpost danpost

2012/9/17

#
What you should have in the lighter act method is something like the following:
public void act()
{
    // code to follow mouse
    if (Greenfoot.isKeyDown("v") && getOneIntersectingObject(tinder.class) != null)
    {
         tinder t = (tinder) getOneIntersectingObject(tinder.class); 
         getWorld().addObject(new burningTinder(), t.getX(), t.getY());
         getWorld().removeObject(t);
    }
}
jhadad jhadad

2012/9/17

#
that is almost identicle to my original coding, but still the mouse freezes once you press v, or any button, and you cant move it around. but then once you let go it all goes back to normal.
danpost danpost

2012/9/17

#
John, can I see what you have for the lighter class so far (the whole thing). Oh, and put the code in the edit box that appear when you click on 'code' below the 'Post a reply' edit box.
danpost danpost

2012/9/17

#
I ran a test scenario using the act method I provided above and everything ran as it was supposed to. Maybe you have other keystroke checks in your program that is freezing it up.
jhadad jhadad

2012/9/17

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class lit here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class lit  extends AnimatedActor
{
   public lit(){
       super("litt", ".png", 3);
    }
    public void act() 
    {
        super.act();
        
          if (Greenfoot.isKeyDown("v") && getOneIntersectingObject(tinder.class) != null)
              {
                  tinder t = (tinder)
                  getOneIntersectingObject(tinder.class);
                  getWorld().addObject(new burningTinder(), t.getX(), t.getY());
                  getWorld().removeObject(t);
                }
        
          if(Greenfoot.mouseMoved(null)) { 
            MouseInfo mouse = Greenfoot.getMouseInfo(); 
            setLocation(mouse.getX(), mouse.getY()); 
        } 
         if(Greenfoot.mouseClicked(this)){
             getWorld().addObject(new lighter(), 500, 100);
             getWorld().removeObject(this);
             
            
            
            }
    
    
    }    
}
danpost danpost

2012/9/17

#
Do you have another class that is either checking for keystrokes or mouse actions?
jhadad jhadad

2012/9/17

#
Only clicks of the mouse, but I've noticed that whenever you press and hold any button, even one not assigned to anything within the program, the mouse stops moving. It will go for a second but then freeze until you release.
There are more replies on the next page.
1
2
3