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

2015/6/15

Shooting Help

Squirrel98 Squirrel98

2015/6/15

#
Hey everyone I have a problem where when my actor is facing to the right it shoots but when my actor is facing to the left it still shoots to the right. here is my code

public void act()
  
      if(isTouching(Flag.class))
        {
            removeTouching(Flag.class);
            World2 lvl1 = new World2();
            Greenfoot.setWorld(lvl1);

 

        if(isTouching(coin.class))
        {
            removeTouching(coin.class);
            Greenfoot.playSound("MarioCoin.mp3");
        }

        if (Greenfoot.isKeyDown("right"))
        {
            move(8);
            marioRight();
            marioRight = true;

        }

        if (Greenfoot.isKeyDown("left"))

        {
            move(-8);
            marioLeft();
            marioLeft = true;

        }
        if("space".equals(Greenfoot.getKey()))
        {
            fire();
        }

        bossMode();

    }   

    public void marioRight()
    {
        frame = frame + 1;
        frame = frame % 4;// 0 -1 -2 
        String switchMario = "MarioSteppingRight"+frame+".png";
        setImage(new GreenfootImage(switchMario));
    }

    public void marioLeft()
    {
        frame = frame + 1;
        frame = frame % 4;// 0 -1 -2 
        String switchBalloon = "MarioLeft"+frame+".png";
        setImage(new GreenfootImage(switchBalloon));

    }

    private void fire()
    {
        Bullet bullet = new Bullet();
        getWorld().addObject(bullet, getX(), getY());

    }
' and this is my bullet code
public void act() 
    {
        move(30);
        World w=getWorld();
        if(isTouching(Snake.class))
        {
            removeTouching(Snake.class);
        }
        if (atWorldEdge())
        {
            w.removeObject(this);
        }
    }    

    public boolean atWorldEdge()
    {
        if(getX() < 5 || getX() > getWorld().getWidth() - 5)
            return true;
        if(getY() < 4 || getY() > getWorld().getHeight() - 5)
            return true;
        else
            return false;

    }
azazeel azazeel

2015/6/15

#
how about change :
    private void fire()
    {
        Bullet bullet = new Bullet();
        getWorld().addObject(bullet, getX(), getY());
		if(marioLeft)
		{
			bullet.turn(180);
		} 
    }
and may be another script you should change
	if (Greenfoot.isKeyDown("right"))
	{
		move(8);
		marioRight();
		marioRight = true;
		marioLeft=false;
	}
	if (Greenfoot.isKeyDown("left"))
	{
		move(-8);
		marioLeft();
		marioLeft = true;
		marioRight = false;
	}
Squirrel98 Squirrel98

2015/6/16

#
Ok thanks i'll try it
Squirrel98 Squirrel98

2015/6/16

#
Ok so a added what you suggested it now shoots in both directions but I can no longer move?
Squirrel98 Squirrel98

2015/6/16

#
Don't worry got it working
Squirrel98 Squirrel98

2015/6/16

#
Thanks for you help
You need to login to post a reply.