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

2013/5/14

Help with rotations

Neon147 Neon147

2013/5/14

#
Im making a game where aliens shoot that will moving on their own, problem is that everytime i am trying to rotate the bullet class south it wont rotate that way it'll only rotate to NE,E, and NW no matter what number i place in setRotation(); I feel like the movement of the aliens is affecting the bullets direction but i dont know. Heres my bullet class, please help? public class AlienLaser extends Actor { /** * Act - do whatever the AlienLaser wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ private int speed = 3; private int point; private World world; private boolean unlock = false; public AlienLaser() { GreenfootSound laser = new GreenfootSound("LaserShot8.wav"); laser.setVolume(75); laser.play(); setRotation(-540); GreenfootImage images = getImage(); images.scale(images.getWidth()-25 , images.getHeight()-25); } public void act() { setLocation(getX() + speed , getY()); move(1); destroy(); if(unlock == false) { if (atWorldEdge() == true) { getWorld().removeObject(this); unlock = false; } } } public boolean atWorldEdge() { if(getX() < 20 || getX() > getWorld().getWidth() - 20) { return true; } if(getY() < 20) { return true; } else { return false; } } public void destroy() { Actor ship = getOneIntersectingObject(spaceShip.class); GreenfootSound explodeAlien = new GreenfootSound("Explode.wav"); explodeAlien.setVolume(75); if(ship != null) { unlock = true; explodeAlien.play(); getWorld().removeObject(ship); getWorld().removeObject(this); } } }
Neon147 Neon147

2013/5/14

#
Oh and heres my AlienA class that is the one shooting public class AlienA extends Actor { /** * Act - do whatever the AlienA wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ private int time = 0; private int delaytime = 50; private int delaytime2 = 75; public AlienA() { GreenfootImage image = getImage(); image.scale(image.getWidth() - 30, image.getHeight()-30); setRotation(0); } public void act() { if(time < 5) { if(delaytime==0) { move(1); time++; delaytime=50; } } if(time>=4) { if(delaytime==0) { setRotation(180); move(1); time++; delaytime=50; } } if(time == 10) { time=0; setRotation(0); } if(Greenfoot.getRandomNumber(10)%3==0) { if(delaytime2==0) { delaytime2=75; getWorld().addObject(new AlienLaser(), getX(), getY()); } delaytime2--; } delaytime--; } }
danpost danpost

2013/5/14

#
The movement of the aliens has no effect on the movement of the laser object. If you remove the 'setLocation' statement in the AlienLaser act method (which means you can also remove the 'speed' field entirely), your laser will go in whatever direction you set for its rotation. If you then replace 'getWorld.addObject(new AlienLaser(), getX(), getY());' with the following:
AlienLaser laser = new AlienLaser();
laser.setRotation(getRotation());
getWorld().addObject(laser, getX(), getY());
Then the alien's rotation will determine the movement direction of the laser.
Neon147 Neon147

2013/5/15

#
It works, thank you!
You need to login to post a reply.