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

2018/3/18

How do i move an object always to one direction with getX and getY?

Recorsi Recorsi

2018/3/18

#
Hello community, i have a little spaceship that shoots lasers. For that i used this code:
private int ProjectileTime;
private static int ShootTime = 15;
public void shoot()
   {
        ProjectileTime++ ;
        if(ProjectileTime >= ShootTime)
            {
               if(Greenfoot.isKeyDown("space"))
            {
                Laser laser = new Laser();
                getWorld().addObject(laser, getX(), getY());
                laser.setRotation(getRotation()-180);
                LaserSound.play();
                ProjectileTime = 0 ;
            }
        }    
   }
The lasers always spawn at the tip of my spaceship. I would like to have two lasers coming out of the front like this: But if i use getX()-2 (for example) and turn the ship, the position is messed up. So how do i fix that? Thanks for the help :)
Game/maniac Game/maniac

2018/3/18

#
after you set the rotation of the laser you can do
laser.move(2);
Recorsi Recorsi

2018/3/18

#
Game/maniac wrote...
after you set the rotation of the laser you can do
laser.move(2);
This doesn't change anything Here is the laser class, maybe this helps:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Laser here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Laser extends SmoothMover
{
    
    public Laser()
    {
        GreenfootImage image = getImage();  
        image.scale(30,8);
        setImage(image);
    }
    public void act() 
    {
        move(15);
        Actor Rock = getOneIntersectingObject(Rock.class);
        if(Rock != null)
        {
           //World Spielfeld = getWorld();
           //Spielfeld.removeObject(Rock);
           //Spielfeld.removeObject(this);
        } else if (isAtEdge()){
            World Spielfeld = getWorld();
            Spielfeld.removeObject(this);
        }
    }
}
Super_Hippo Super_Hippo

2018/3/18

#
Try this:
Laser laser1=new Laser(), laser2=new Laser();
getWorld().addObject(laser1, getX(), getY());
getWorld().addObject(laser2, getX(), getY());
laser1.setRotation(getRotation()-180);
laser2.setRotation(getRotation()-180);
laser1.turn(90); laser1.move(3); laser1.turn(-90);
laser2.turn(-90); laser2.move(3); laser2.turn(90);
Recorsi Recorsi

2018/3/18

#
Super_Hippo wrote...
Try this:
Laser laser1=new Laser(), laser2=new Laser();
getWorld().addObject(laser1, getX(), getY());
getWorld().addObject(laser2, getX(), getY());
laser1.setRotation(getRotation()-180);
laser2.setRotation(getRotation()-180);
laser1.turn(90); laser1.move(3); laser1.turn(-90);
laser2.turn(-90); laser2.move(3); laser2.turn(90);
move(
Thank you it works :)
You need to login to post a reply.