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

2016/5/7

Access rotation of another actor?

AdamH04 AdamH04

2016/5/7

#
So I have a scenario ive been working on in which there is a plane which you control and then a few other enemy planes which move randomly, I intend to eventually make bullets be able to fire from the plane and damage the enemies. So far I am stuck at getting the bullet to face the same direction as the plane. This is the code I have so far
//Plane script
import greenfoot.*;


public class Plane extends Actor
{   
    int speed = 0;
    int bombPlaced = 0;

    public Plane(){
    GreenfootImage image = getImage();
    image.scale(75, 75);
    setImage(image);
    }

    public void act() 
    {
        rotation = getRotation();
        bombPlaced = bombPlaced + 1;
        if (Greenfoot.isKeyDown("left")){
            turn(-5);
        }
        if (Greenfoot.isKeyDown("right")){
            turn(5);
        }
        if (Greenfoot.isKeyDown("up") && speed < 65){
            speed = speed + 1;

        }
        
        if (Greenfoot.isKeyDown("down") && speed >= 0){
            speed = speed - 1;
        }
        move(speed / 10);
        if ((speed > 0) && !(Greenfoot.isKeyDown("up"))){
            speed = speed - 1;
        }
        if (Greenfoot.isKeyDown("space") && bombPlaced > 40){
            getWorld().addObject(new Bomb(), getX(), getY());
            bombPlaced = 0;
        }  
        if (Greenfoot.isKeyDown("shift")){
            getWorld().addObject( new Bullet(), getX(), getY());
        }
        }
    }   
//Bullet script
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)


public class Bullet extends Actor
{
    int x = 0;

    
    public void act() 
    {
        if (x == 0){
            turnPlaneDirection();// method which should eventually return the rotation of Plane
        }
                
        move(10);
    } 

    }

  
//World script (called sea)
import greenfoot.*;
import java.util.*;

public class Sea extends World
{


    public Sea()
    {    

        super(1000, 520, 1); 
        addObject( new EnemyPlane(), 100, 100);
        addObject( new EnemyPlane(), 200, 500); 
        addObject( new EnemyPlane(), 50, 425);
        addObject(Plane, 180, 180);
        
    }

}
I have 2 others which are the enemy plane script and a bomb script to drop bombs for a later feature. Please if someone could tell me how I can make the bullet be at the same rotation of the plane at the time of firing. any help will be appreciated, let me know if you don't understand whats going on.
davmac davmac

2016/5/7

#
You should have the plane set the rotation of the bullet, rather than have the bullet try to set its rotation to that of the plane. So, your Plane class line 43 should become:
            Bullet bullet = new Bullet();
            getWorld().addObject(bullet, getX(), getY());
            bullet.setRotation(getRotation());
AdamH04 AdamH04

2016/5/8

#
Thanks so much!
You need to login to post a reply.