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

2017/1/19

Shooting Actor heeelp

Hondrok Hondrok

2017/1/19

#
I'm trying to make shoot the Sphere_left every 3 seconds
import greenfoot.*;

public class Boss extends Actor
{
    private int a = 100;
    private int i = 0;
    
    public void act() 
    {
        boss_attack();
    }
    
    public void boss_attack()
    {
        while(i<a)
        {
            if(i%3==0)
            {
                getWorld().addObject(new Sphere_left(), getX(), getY());
            }
            
            if(i==99)
            {
                i=0;
            }
            
            i++;
        }
        
        
 
    }
}
danpost danpost

2017/1/19

#
The code between lines 15 and 28 are executed all at once (during the execution of one act cycle). That is the nature of a loop (a 'while' loop, in this case). On top of that, the value of 'i' will never reach the value of 'a' (100) because you reset it back to zero if when it reaches 99. Therefore, the loop continue indefinitely (until you terminate the program or you get an OutOfMemory error message, whichever is first). Increment 'i' unconditionally (once per act cycle); use a single 'if' statement to check for spawning; reset 'i' to zero only when spawning; adjust 'a' to suit (probably somewhere between 165 and 180 would be about 3 seconds).
Hondrok Hondrok

2017/1/19

#
danpost wrote...
The code between lines 15 and 28 are executed all at once (during the execution of one act cycle). That is the nature of a loop (a 'while' loop, in this case). On top of that, the value of 'i' will never reach the value of 'a' (100) because you reset it back to zero if when it reaches 99. Therefore, the loop continue indefinitely (until you terminate the program or you get an OutOfMemory error message, whichever is first). Increment 'i' unconditionally (once per act cycle); use a single 'if' statement to check for spawning; reset 'i' to zero only when spawning; adjust 'a' to suit (probably somewhere between 165 and 180 would be about 3 seconds).
Can you be more explicit please?
danpost danpost

2017/1/19

#
Hondrok wrote...
Can you be more explicit please?
It seems what I gave was pretty much it. If you have a specific question about what I gave, ask it.
Hondrok Hondrok

2017/1/19

#
danpost wrote...
Hondrok wrote...
Can you be more explicit please?
It seems what I gave was pretty much it. If you have a specific question about what I gave, ask it.
How do I increment the 'i' unconditionally?
Super_Hippo Super_Hippo

2017/1/19

#
Simply do 'i++;' without a condition, so without if or while.
Hondrok Hondrok

2017/1/20

#
danpost wrote...
The code between lines 15 and 28 are executed all at once (during the execution of one act cycle). That is the nature of a loop (a 'while' loop, in this case). On top of that, the value of 'i' will never reach the value of 'a' (100) because you reset it back to zero if when it reaches 99. Therefore, the loop continue indefinitely (until you terminate the program or you get an OutOfMemory error message, whichever is first). Increment 'i' unconditionally (once per act cycle); use a single 'if' statement to check for spawning; reset 'i' to zero only when spawning; adjust 'a' to suit (probably somewhere between 165 and 180 would be about 3 seconds).
like this?
import greenfoot.*;

public class Boss extends Actor
{
    private int a = 51;
    private int i = 0;
    
    public void act() 
    {
        boss_attack();
    }
    
    public void boss_attack()
    {
        i++;
        while(i<a)
        {
            if(i<165)
            {
                getWorld().addObject(new Sphere_left(), getX(), getY());
            }
            
            if(i==50)
            {
                i=0;
            }
        }
        
        
 
    }
}

Super_Hippo Super_Hippo

2017/1/20

#
Try this:
i++;
if (i == 180)
{
    getWorld().addObject(new Sphere_left(), getX(), getY());
    i = 0;
}
Hondrok Hondrok

2017/1/20

#
Super_Hippo wrote...
Try this:
i++;
if (i == 180)
{
    getWorld().addObject(new Sphere_left(), getX(), getY());
    i = 0;
}
yeah it works thank youu
You need to login to post a reply.