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

2013/10/29

How do track rotation from actor to mouse

i_joker123 i_joker123

2013/10/29

#
I am trying to fire bullets from the location of the gun that is firing. So when the mouse is moved the gun moves at a direction. I would like the bullet to move at the direction which the gun is pointing to. So i can fire at any direction. Ive tried using the turntowards() method. but the bullets only shot to the right of the screen despite where it is rotated. Any suggestions? I have a character class : import greenfoot.*; public class Gun extends Actor { private int speed; public void act() { // Add your action code here. MouseInfo mouse = Greenfoot.getMouseInfo(); if (mouse !=null) setRotation((int)(180*Math.atan2(mouse.getY()-getY(),mouse.getX()-getX())/Math.PI)); move(speed); if(Greenfoot.mouseClicked(null)) { getWorld().addObject(new bullet(getRotation()),getX(),getY()); turnTowards(mouse.getX(), mouse.getY()); } } } I have a bullet class : import greenfoot.*; public class bullet extends Actor { private int direction; public void act() { setLocation(getX()+5,getY()); } public bullet(int dir) { this.direction=dir; } } And i have a baddie class : import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class balloon here. * * @author (your name) * @version (a version number or a date) */ public class baddie extends Actor { public void act() { setLocation(getX(), getY()-1); } }
danpost danpost

2013/10/29

#
Line 10 in the Bullet class determines where you bullet will go. It says your bullet should go to the right 5 pixels every act cycle. Hence, what you see.
i_joker123 i_joker123

2013/10/29

#
Could you suggest code to improve this?
danpost danpost

2013/10/29

#
Set the rotation of the bullet in its constructor and use 'move(5)' instead of 'setLocation...'.
i_joker123 i_joker123

2013/10/29

#
ive done that but no changes happens
danpost danpost

2013/10/29

#
Please re-post your bullet class.
i_joker123 i_joker123

2013/10/29

#
public class bullet extends Actor { private int direction; public void act() { move(5); } public bullet(int dir) { this.direction=dir; } }
danpost danpost

2013/10/29

#
I do not see you setting the rotation of the bullet anywhere (setting the value of a field does not set the objects rotation).
i_joker123 i_joker123

2013/10/29

#
i dont understand?
danpost danpost

2013/10/29

#
The statement 'this.direction = dir;' just sets the value of a field for the bullet (it assigns the value of 'dir' to be the value of the int field 'direction'). It does not change the rotation of the object itself which must be done with the 'turn', 'turnTowards' or 'setRotation' method.
i_joker123 i_joker123

2013/10/29

#
I see, i have tried the following: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) public class bullet extends Actor { private int direction; /** * Act - do whatever the firedArrow wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { // Add your action code here. //setLocation(getX()+5,getY()); } public bullet(int dir) { this.direction=dir; setRotation((int)(180*Math.atan2(this.getY()-getY(),this.getX()-getX())/Math.PI)); } } but this time i get an error
danpost danpost

2013/10/29

#
Just use 'setRotation(dir);'. And replace the 'setLocation' statement with the 'move' one.
i_joker123 i_joker123

2013/10/29

#
Thank you so much! I appreciate your time :) Thanks again!
You need to login to post a reply.