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

2018/12/26

Bullet moving in direction that actor is facing

Pando Pando

2018/12/26

#
So when i shoot the bullet while im facing right it shoots right and faces right. When i turn my character left and shoot, it faces left but shoota right still. Any reccomendations?
Pando Pando

2018/12/26

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Ball here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Ball extends Actor
{
    GreenfootImage right = new GreenfootImage("Fireball.png");
    GreenfootImage left = new GreenfootImage("FireballLeft.png");
        
    private int speed = 10;
    private int damage = 20;
    
    public Ball()
    {
        GreenfootImage fireBall = getImage();
        fireBall.scale(80, 80);
    }
    /**
     * Act - do whatever the Ball wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        setLocation(getX() + speed, getY());
        hurtEnemy();
        if (getWorld() != null)
        {
            if (checkBoundaries() == true)
            {
                getWorld().removeObject(this);
            }
        }
    }    
    
    public boolean checkBoundaries()
    {
        if (getX() < 60 || getX() > getWorld().getWidth() - 60)
        {
            return true;
        }
        
        if (getY() < 60 || getY() > getWorld().getHeight() - 60)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    
    public void hurtEnemy()
    {
        Snake snake = (Snake) getOneObjectAtOffset(0, 0, Snake.class);
        if (snake != null)
        {
            Explode explosion = new Explode();
            getWorld().addObject(explosion, getX(), getY());
            getWorld().removeObject(this);
            snake.setHealth(damage);
        }
    } 
}
Pando Pando

2018/12/26

#
public class Knight extends Actor
{
    GreenfootImage right = new GreenfootImage("knightRight.png");
    
    private int ySpeed;
    private int speed;
    private int jumpStrength;
    private int acceleration;
    private int time;
    private int direction;
    private int fireballSpeed;
    
    public Knight()
    {
        GreenfootImage right = getImage();
        right.scale(100, 100);
        
        ySpeed = 0;
        speed = 3;
        jumpStrength = -16;
        acceleration = 1;
        time = 0;
        direction = 0;
        fireballSpeed = 10;
    }
    
    public void act() 
    {
        moveAndTurn();
        direction();
        checkFall();
    }
    
    public void moveAndTurn()
    {
        if (Greenfoot.isKeyDown("A"))
        {
            if (direction == 0)
            {
                turn(180);
                getImage().mirrorVertically();
            }
            setLocation(getX() - speed, getY());
            direction = 1;
        }
         
        if (Greenfoot.isKeyDown("D"))
        {
            if (direction == 1)
            {
                turn(180);
                getImage().mirrorVertically();
            }
            setLocation(getX() + speed, getY());
            direction = 0;
        }
        
        if (Greenfoot.isKeyDown("space"))
        {
            jump();
        }
        
        if (Greenfoot.isKeyDown("c"))
        {
            shoot();
        }
    }
    
    public void jump()
    {
        if (Greenfoot.isKeyDown("space") && getY() == 380)
        {
            ySpeed = jumpStrength;
            fall();
        }
        
    }
    
    public void fall()
    {
        setLocation(getX(), getY() + ySpeed);
        ySpeed = ySpeed + acceleration;
    }
    
    public void checkFall()
    {
        if (getY() == 380)
        {
            ySpeed = 0;
        }
        else
        {
            fall();
        }
    }
    
    public void shoot()
    {
        if (time <= 0)
            {
                Ball fireball = new Ball();
                if (direction == 1)
                {
                    getWorld().addObject(fireball, getX(), getY());
                    fireball.getImage().mirrorVertically();
                    fireball.turn(180);
                }
                
                if (direction == 0)
                {
                    getWorld().addObject(fireball, getX(), getY());
                }
                time = 20;
            }
            
            else
            {
                time --;
            }
    }
    
    public int direction()
    {
        return direction;
    }
}
Pando Pando

2018/12/26

#
nvm i used found it out by using getRotation() in bullet class and changing speed (-/+) based on angle
You need to login to post a reply.