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

2013/6/1

Multiple Actors

alagon alagon

2013/6/1

#
I can't spawn multiple functioning actors within my world. My actor currently is a bomb that is supposed to fall from the sky. If i put in 2 and press "Run" only 1 of em does what it is supposed to do, once it reaches the bottom of the screen, it explodes and leaves the other bomb behind. If i press run and put in 3 bombs, they all work to a certain extent, being that each one of em move 1 then the next one moves and then the next one, turn by turn. Help?
danpost danpost

2013/6/1

#
Are you still trying to use the 'delay' statement (after I told you that you should not)?
alagon alagon

2013/6/2

#
Well here's my code: For the bomb thing:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public class Bomb extends Actor
{
 
public void act() 
    if (getY()<500)
    
        for(int z = 1; z<13; z++)
        {   
            setImage("Missile/"+ z +".png");
            Greenfoot.delay(3);
            setLocation(getX(), getY()+1); 
        }
    
    else
    
         for(int z = 1; z<25; z++)
        {   
            setImage("Explosion/"+ z +".png");
            Greenfoot.delay(3);
        }
        getWorld().removeObject(this);
    
}
}
alagon alagon

2013/6/2

#
I read your explanation multiple times, but i still don't quite understand it.
danpost danpost

2013/6/2

#
Ok, please understand this. (1) when a 'delay' statement is executed, NO statements will be executed in your scenario until the delay time has elapsed; and, (2) each act method must complete its execution before any other actor (or active object, like the world) can have their act methods executed. During the normal course of a standard speed scenario, all act methods (those for all objects in the world and the one for the world itself) will be executed about 60 times per second. This is what gives 'life' to the scenario. It is a built-in looping among all the actions that need to be performed. If you tell an object to move up one pixel in its act method, it will try to move up one pixel about sixty times every second. Running a loop to change the image of an object 12 times in an act method is like telling the object to change its image to the 12th image immediately. Example (using rotation)
1
2
3
4
5
6
7
8
9
10
11
12
public void act()
{
    for (int i=0; i<180; i++)
    {
        setRotation(getRotation()+1);
    }
}
// is equivalent to saying
public void act()
{
    setRotation(180);
}
Once the first act method finishes executing, the result will be the same as if the second one executed. Both are executed in one act cycle.
alagon alagon

2013/6/2

#
Alright, i removed the delay, and it has worked :D thank you very much! But there is another problem...the bomb won't explode once it is at the bottom...it just disappears..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public class Bomb extends Actor
{
int animation = 1;
public void act() 
    if (getY()<500)
    {
        {   
            setImage("Missile/"+ z +".png");
            setLocation(getX(), getY()+1); 
            animation++;
            if(animation ==12)
            {
                animation=1;
            }   
        }
    
    else
    
        {  
            animation = 1;
            setImage("Explosion/"+ animation +".png");
        }
        getWorld().removeObject(this);
    
}
}
the integer animation is for the amount of frames, the explosion having 25 and the normal movement having 12. How do i make it explode?
alagon alagon

2013/6/2

#
Whoops, forgot to remove the Z in the Missile dropping thing...it's supposed to be the "animation" integer.
You need to login to post a reply.