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

2014/4/21

Problems with moving in an uploaded scenario

jogit666 jogit666

2014/4/21

#
I recently uploaded a scenario called UFO hunt . The problem is, the tank and the tank's cannon move very strangely with the keys, even though on the computer it was fine. Tank's movement:
 public int pspeed = 3;
    public void act()
    {
        pmove();
        pmakeSmoke();
    }    
    
    public void pmove()
    {
        if (Greenfoot.isKeyDown("d"))
        {
            move(pspeed);
        }
        if (Greenfoot.isKeyDown("a"))
        {
            move(-pspeed);
        }
    }   
Cannon's movement and turning: Notice: gspeed is the speed that the gun moves left or right, it is equal to the speed the tank moves (pspeed). the turn() method activates from the move() method. the gunAngle thing is so that there is a limit on how much you can turn the cannon.
    public int gspeed = pspeed;
    public int gTurnspeed = 2;
    
    public Gun()
    {
        setRotation(270);
    }
    
    public void act() 
    {
        move();
        shoot();
    }    
    
    public void move()
    {
        if (Greenfoot.isKeyDown("a"))
        {
            setLocation(getX()-gspeed,getY());
        }
        if (Greenfoot.isKeyDown("d"))
        {
            setLocation(getX()+gspeed,getY());
        }
        
        turn();
    }
    
    public void turn()
    {
        int gunAngle = getRotation();
        
        if (gunAngle < 358)
        {
            if (Greenfoot.isKeyDown("right"))
            {
                turn(gTurnspeed);
            }
        }
        
        if (gunAngle > 210)
        {
            if (Greenfoot.isKeyDown("left"))
            {
                turn(-gTurnspeed);
            }
        }
    }
Thanks!
danpost danpost

2014/4/21

#
I would be best if the tank moved the gun also.
public void pmove()
{
    Actor gun = getOneIntersectingObject(Gun.class);
    int dx = 0;
    if (Greenfoot.isKeyDown("d"))
    {
        dx += pspeed;        
    }
    if (Greenfoot.isKeyDown("a"))
    {
        dx -= pspeed;
    }
    if (dx == 0) return;
    setLocation(getX()+dx, getY());
    if (gun != null) gun.setLocation(gun.getX()+dx, gun.getY());
}
Then remove the 'move' method and its call from the 'act' method from the Gun class.
jogit666 jogit666

2014/4/21

#
Thanks! Wow, a lot of this stuff is new for me, so I just copied and it works. I also updated it on the web. Yeah it is better, now at least the tank moves constantly, still not good enough though. There is also a problem with turning the gun. On my laptop everything works perfectly
danpost danpost

2014/4/22

#
I forgot, you need to call 'turn' from the 'act' method.
jogit666 jogit666

2014/4/22

#
Yes, I realized. And never mind, it works on a different computer, I guess its a mac/safari problem.
You need to login to post a reply.