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

2017/2/21

Projectile delay problem :')

joshuadswa joshuadswa

2017/2/21

#
Hello again :D I have some problem again here ( i hope you guys not tired helping me xD) so i want when i click the mouse the object will appear and then it will delayed before my actor can shoot again, but in my case when i click the mouse it will not appear(i need click many times to make it appear) and lets assume i change it to isKeyDown("Space"), on the first time i press the Space it will be delayed first before my actor can shoot .-. here's my code
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Hero1 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Hero1 extends Actor
{
    /**
     * Act - do whatever the Hero1 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    boolean ready = true;
    int loading = 1;
    int step = 1;
    public void act() 
    {
        heroMovement();
        turntoMouse();
        shootDelay();
        delayA();
        step++;
    }    
    
    public void heroMovement()
    {
      int dx=0, dy=0;
      if (Greenfoot.isKeyDown("w")) dy--;
      if (Greenfoot.isKeyDown("s")) dy++;
      if (Greenfoot.isKeyDown("d")) dx++;
      if (Greenfoot.isKeyDown("a")) dx--;
     
      // if(Greenfoot.mouseClicked(getWorld()))
      //{
       // getWorld().addObject(new Projectile2(getRotation()),getX(),getY());
      //}
      setLocation(getX()+dx*5, getY()+dy*5);
    }
    public void turntoMouse()
    {
        MouseInfo mouse = Greenfoot.getMouseInfo();
        if(mouse!=null)
        {
            turnTowards(mouse.getX(),mouse.getY());
        }
    }
    public void shootDelay()
    {
        if(ready == true)
        {  
             if (Greenfoot.mouseClicked(getWorld()))
            {
                getWorld().addObject(new Projectile2(getRotation()),getX(),getY());
            }

        }
        ready = false;
    }
    public void delayA()
    {
        if(step%40==1)
        {
            ready = true;
        }
    }
}
Super_Hippo Super_Hippo

2017/2/21

#
private int shotDelay=0;
private final int shotDelayMax = 100; //the higher the number, the longer the delay between shots

public void act()
{
    heroMovement();
    turntoMouse();
    shoot();
}

private void shoot()
{
    if (shotDelay>0 && --shotDelay>0) return;
    if (Greenfoot.mousePressed(null))
    {
        getWorld().addObject(new Projectile2(getRotation()),getX(),getY());
        shotDelay = shotDelayMax;
    }
}
Changed mouseClicked to mousePressed → you shoot when the button is pressed and not when it is released Changed getWorld to null → you shoot when you press anything and not only when you click the world background (=no object)
joshuadswa joshuadswa

2017/2/22

#
How about if i change mousePressed to Greenfoot.isKeyDown("space") using my code?
Super_Hippo Super_Hippo

2017/2/22

#
The problem about your code is that you increase 'step' every act cycle. 'ready' is true every 40 act cycles, so if you press space or your mouse at the correct moment, you can shoot without delay, but that is not very likely.
joshuadswa joshuadswa

2017/2/22

#
yeah >.< i change the movement speed on another class, and it work xD Thx a lot :D ^^
You need to login to post a reply.