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

2021/12/12

Need change direction of shooting

greenwood greenwood

2021/12/12

#
how to shoot fireball from direction my actor facing
 import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)


public class Wraith extends Actor
{
    int animateImage = 0;   //for walking animation image (frame)
    int animateSpeed = 3;   //lower the animate speed for faster walking animation
    int count;
    SimpleTimer spaceTimer = new SimpleTimer();  //timer for when the next space can be tekan lagi
    
    public Wraith()
    {
       setImage("Wraith_01_Moving Forward_000.png");
       getImage().scale(260,210);
    }
    
    public void act()
    {
        count++;
        checkKeys();
        
        //for movement restrict of wraith in the world
        if(getX()>1200) setLocation(1200,getY());
        if(getY()>550) setLocation(getX(), 550);
        if(getY()<250) setLocation(getX(),250);
        
    }
    
    private void checkKeys()
    {
        if(Greenfoot.isKeyDown("w")) //for moveup
        {
            setLocation(getX(), getY()-5);
        }
        
        if(Greenfoot.isKeyDown("s")) //for movedown
        {
            setLocation(getX(), getY()+5);
        }
        
        if(Greenfoot.isKeyDown("d")) //for move right
        {
            setLocation (getX() +5, getY());
            walkinganimateRight();
        }
        
        if(Greenfoot.isKeyDown("a")) //for move  left
        {
            setLocation (getX() -5, getY());
            walkinganimateLeft();
        }
        
        if(Greenfoot.isKeyDown("space") && spaceTimer.millisElapsed() > 1000)
        {
            shoot();
            spaceTimer.mark();   //reset balik timer kepada 0
        }
        
        /*if(Greenfoot.isKeyDown(null))  //kalau tak tekan apa
        {
            
        }*/
    }
    
    public void walkinganimateRight()
    {
        if(count % animateSpeed == 0)
        {
            if(animateImage >11)
            {
                animateImage = 0;
            }
            setImage("Wraith_01_Moving Forward_00" + animateImage + ".png");
            animateImage++;
            getImage().scale(260,210);
            
        }
    }
    
    public void walkinganimateLeft()
    {
        if(count % animateSpeed ==0)
        {
            if(animateImage >11)
            {
                animateImage = 0;
            }
            setImage("Wraith_01_Moving Forward_00" + animateImage + ".png");
            animateImage++;
            getImage().scale(260,210);  
            getImage().mirrorHorizontally();
        }
    }
    
    public void shoot()
    {
        Fireball fireball = new Fireball();
        getWorld().addObject(fireball, getX()+100, getY());        //masukkan fireball ke dalam world dan berjarak 100 pixels dari wraith
        fireball.setRotation(getRotation());
    }
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)


public class Fireball extends Actor
{
    public Fireball()
    {
        setImage("blueFireball.png");
        getImage().scale(80,80);
    }
    
    public void act()
    {
        move(10);
    }
}
Super_Hippo Super_Hippo

2021/12/12

#
The ‘getRotation’ only works if you turn your character around by rotating it. But you don’t. You only mirror the image. You could save the current direction in a variable and then turn the fireball if needed.
greenwood greenwood

2021/12/12

#
Super_Hippo wrote...
The ‘getRotation’ only works if you turn your character around by rotating it. But you don’t. You only mirror the image. You could save the current direction in a variable and then turn the fireball if needed.
I try this but the fireball doesn't move at all. What should I do ?
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)


public class Wraith extends Actor
{
    int animateImage = 0;   //for walking animation image (frame)
    int animateSpeed = 3;   //lower the animate speed for faster walking animation
    int animateSpeed2 = 6;  //lower the animate speed for faster idle animation
    int animateSpeed3 = 1; //speed for shooting animation
    int count;
    int direction =1; //arah Wraith tengok
    SimpleTimer spaceTimer = new SimpleTimer();  //timer for when the next space can be tekan lagi
    
    public Wraith()
    {
       setImage("Wraith_01_Idle Blinking_000.png");
       getImage().scale(260,210);
    }
    
    public void act()
    {
        count++;
        checkKeys();
        
        //for movement restrict of wraith in the world
        if(getX()>1200) setLocation(1200,getY());
        if(getY()>550) setLocation(getX(), 550);
        if(getY()<250) setLocation(getX(),250);
        
    }
    
    private void checkKeys()
    {
        if(Greenfoot.isKeyDown("w")) //for moveup
        {
            setLocation(getX(), getY()-5);
        }
        
        if(Greenfoot.isKeyDown("s")) //for movedown
        {
            setLocation(getX(), getY()+5);
        }
        
        if(Greenfoot.isKeyDown("d")) //for move right
        {
            setLocation (getX() +5, getY());
            walkinganimateRight();
            direction =1;
        }
        
        if(Greenfoot.isKeyDown("a")) //for move  left
        {
            setLocation (getX() -5, getY());
            walkinganimateLeft();
            direction =2;
        }
        
        if(Greenfoot.isKeyDown("space") && spaceTimer.millisElapsed() > 300)
        {
            if(direction==1)
            {
                setImage("Wraith_01_Casting Spells_006.png");
                getImage().scale(260,210);
                shoot();
            }
            
            else if(direction==2)
            {
                setImage("Wraith_01_Casting Spells_006.png");
                getImage().scale(260,210);
                getImage().mirrorHorizontally();
                shoot(); 
            }
            spaceTimer.mark();   //reset balik timer kepada 0
        }
        
        //declare for no button pressed
        boolean up = Greenfoot.isKeyDown("w");
        boolean down = Greenfoot.isKeyDown("s");
        boolean right = Greenfoot.isKeyDown("d");
        boolean left = Greenfoot.isKeyDown("a");
        boolean space = Greenfoot.isKeyDown("space");
        if(!up && !down && !left && !right && !space)  //no button is pressed
        {
            if(direction ==1)
            {
                idleanimationRight();
            }
            
            if(direction ==2)
            {
                idleanimationLeft();
            }
        }
    }
    
    public void walkinganimateRight()
    {
        if(count % animateSpeed == 0)
        {
            if(animateImage >11)
            {
                animateImage = 0;
            }
            setImage("Wraith_01_Moving Forward_00" + animateImage + ".png");
            animateImage++;
            getImage().scale(260,210);
            
        }
    }
    
    public void walkinganimateLeft()
    {
        if(count % animateSpeed ==0)
        {
            if(animateImage >11)
            {
                animateImage = 0;
            }
            setImage("Wraith_01_Moving Forward_00" + animateImage + ".png");
            animateImage++;
            getImage().scale(260,210);  
            getImage().mirrorHorizontally();
        }
    }
    
    public void shoot()
    {
        Fireball fireball = new Fireball();
        getWorld().addObject(fireball, getX()+100, getY());        //masukkan fireball ke dalam world dan berjarak 100 pixels dari wraith
        
        if(direction==1)
        {
            fireball.move(5);
        }
        
        else if(direction==2)
        {
            fireball.move(-5);
        }
    }
    
    public void idleanimationRight()
    {
        if(count % animateSpeed2 ==0)
        {
            if(animateImage >11)
            {
                animateImage =0;
            }
            setImage("Wraith_01_Idle Blinking_00" +animateImage + ".png");
            animateImage++;
            getImage().scale(260,210);
        }
    }
    
        public void idleanimationLeft()
    {
        if(count % animateSpeed2 ==0)
        {
            if(animateImage >11)
            {
                animateImage =0;
            }
            setImage("Wraith_01_Idle Blinking_00" +animateImage + ".png");
            animateImage++;
            getImage().scale(260,210);
            getImage().mirrorHorizontally();
        }
    }
    

}
my Fireball
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)


public class Fireball extends Actor
{
    int animateImage =0;
    int animateSpeed2 = 6; //animation speed
    int count;
    
    public Fireball()
    {
        //setImage("blueFireball.png");
        setImage("frame_0_delay-0.1s.png");
        getImage().scale(150,150);
    }
    
    public void act()
    {
        count++;
        animate();
    }
    
    public void animate()
    {
        if(count % animateSpeed2 ==0)
        {
            if(animateImage >5)
            {
                animateImage =0;
            }
            setImage("frame_" +animateImage + "_delay-0.1s"+ ".png");
            animateImage++;
            getImage().scale(150,150);
        }     
    }
}
greenwood greenwood

2021/12/12

#
nvm i already foun the solution.
        Fireball fireball = new Fireball();
        if(direction==1)
        {
            getWorld().addObject(fireball, getX()+100, getY());        //masukkan fireball ke dalam world dan berjarak 100 pixels dari wraith
        }
        
        else if(direction==2)
        {
            getWorld().addObject(fireball, getX()-100, getY());        //masukkan fireball ke dalam world dan berjarak 100 pixels dari wraith
            fireball.setRotation(180);
        }
greenwood greenwood

2021/12/12

#
i have another problem. How to limit the number of zombie spawns ? Let say I want only 15 zombies to spawn my World class
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class MyWorld extends World
{
    int count=0;
    int spawnSpeed = 100;  //control the spawn speed, lower if you want faster spawn
    int randomSpawn;
    
    public MyWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(1200, 720, 1); //size backround
        addObject(new Wraith(),(int)(0.5*getWidth()), (int)(0.5*getHeight()));  //wraith letak kat tengah2
    }
    
    public void act()
    {
        count++;
        spawnZombie1();
    }
    
    private void spawnZombie1()
    {
            if(count% spawnSpeed ==0)
        {
            randomSpawn = Greenfoot.getRandomNumber(4);
            switch (randomSpawn) 
            {
                case 0 : addObject(new Zombie1(), 1200, (int)(0.6*getHeight())); break;
                case 1 : addObject(new Zombie1(), 1200, (int)(0.4*getHeight())); break;
                case 2 : addObject(new Zombie1R(), 0, (int)(0.6*getHeight())); break;
                case 3 : addObject(new Zombie1R(), 0, (int)(0.4*getHeight())); break;
            }
        }
    }
}
danpost danpost

2021/12/13

#
greenwood wrote...
i have another problem. How to limit the number of zombie spawns ? Let say I want only 15 zombies to spawn
If you want no more than 15 in the world at one time, you can use:
if (getObjects(Zombie1.class).size() < 15)
to limit spawning. If you want only 15 to spawn in total, then add a counter field and use it to limit the spawning:
private int zombiesSpawned;

private void spawnZombie1()
{
    if (zombiesSpawned == 15) return;
    if (count%spawnSpeed == 0)
    {
        zombiesSpawned++;
        ... // lines 25 on as above
... //
You need to login to post a reply.