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

2014/1/3

how to stop object from turning toward mouse

deathburrito16 deathburrito16

2014/1/3

#
I want the bullet to turn toward mouse, then travel straight. with my current code, it only does that with the mouse not moving. here is the bullet code.
     if (Greenfoot.mouseMoved(null))
        {
        turnTowards((Greenfoot.getMouseInfo().getX()), (Greenfoot.getMouseInfo().getY()));
    }
        move(15);
        Actor Player;
        Player = getOneObjectAtOffset(0, 0, Player.class);
        if (Player != null)
        {
            World world;
            world = getWorld();
            world.removeObject(Player);
            Deadman deadman = new Deadman();
            getWorld().addObject(deadman, getX(), getY());
    }    
Actor enemy;
enemy = getOneObjectAtOffset(0, 0, Enemy.class);
if (enemy != null)
{
    World world;
    world = getWorld();
    world.removeObject(enemy);
    Deadenemy deadenemy = new Deadenemy();
    getWorld().addObject(deadenemy, getX(), getY());
}
}
}
erdelf erdelf

2014/1/3

#
well, u should write the turnTowards call not in the act method but in the addedToWorld method, then it should work
deathburrito16 deathburrito16

2014/1/4

#
how?
erdelf erdelf

2014/1/4

#
remove line 1-4 and write line 3 in the addedToWorld method
danpost danpost

2014/1/4

#
What is the control (trigger) to determine when to fire a bullet at the mouse?
deathburrito16 deathburrito16

2014/1/4

#
to erdelf, a don't understand how to add to the addToWorld method. to danpost, the trigger is "m". now that you mention it, is there a way to have it fire when the mouse is clicked?
danpost danpost

2014/1/4

#
deathburrito16 wrote...
to danpost, the trigger is "m". now that you mention it, is there a way to have it fire when the mouse is clicked?
Yes, and that would be a good way to go because the MouseInfo object will not be null at that time. Remove lines 1 through 4 from the code of your Bullet class supplied above. Then, when creating the bullet (not in the Bullet class):
if (Greenfoot.mouseClicked(null))
{
    MouseInfo mouse = Greenfoot.getMouseInfo();
    Bullet bullet = new Bullet();
    // add 'bullet' into world
    bullet.turnTowards(mouse.getX(), mouse.getY());
}
deathburrito16 deathburrito16

2014/1/4

#
nope.
deathburrito16 deathburrito16

2014/1/4

#
didn't add in bullet
danpost danpost

2014/1/4

#
I commented line 5 for you to put the code for adding the bullet into the world there. The code depends on (1) where in your code the adding of the bullet into the world is; and (2) what location you with to add the bullet into the world. I was not going to guess as to what you wanted.
deathburrito16 deathburrito16

2014/1/5

#
oh. I feel like an idiot
You need to login to post a reply.