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

2021/1/4

Actor keeps freezing.

ItzLukeStorm ItzLukeStorm

2021/1/4

#
I'm not sure if this is related, but whenever the actor's variable "enterTime" reaches 2 on the way back up, the actor freezes. Much Appreciated! Here is the code for the actor the keeps freezing.
import greenfoot.*;

public class Portal extends Actor
{
    private int enterTime = 20;
    private int allAboard = 600;
    private int enter = 1;

    public void act() 
    {
        if(enter == 1)
        {
            enterTime--;
        }
        if(enter == 2)
        {
            enterTime++;
        }
        if(enterTime > 20)
        {
            enterTime = 20;
        }
        leave();
        entrance();
        rotation();
    }

    public void leave()
    {
        allAboard--;
        if(allAboard <= 0)
        {
            enterTime = 1;
            enter = 2;
        }
        if(enterTime == 20)
        {
            getWorld().removeObject(this);
        }
    }

    public void rotation()
    {
        if(enterTime <= 0)
        {
            turn(3);
        }
    }

    public void entrance()
    {
        if (enterTime == 19 || enterTime == 20)
        {
            setImage("portal1.png");
        }
        if (enterTime == 17 || enterTime == 18)
        {
            setImage("portal2.png");
        }
        if (enterTime == 15 || enterTime == 16)
        {
            setImage("portal3.png");
        }
        if (enterTime == 13 || enterTime == 14)
        {
            setImage("portal4.png");
        }
        if (enterTime == 11 || enterTime == 12)
        {
            setImage("portal5.png");
        }
        if (enterTime == 9 || enterTime == 10)
        {
            setImage("portal6.png");
        }
        if (enterTime == 7 || enterTime == 8)
        {
            setImage("portal7.png");
        }
        if (enterTime == 5 || enterTime == 6)
        {
            setImage("portal8.png");
        }
        if (enterTime == 3 || enterTime == 4)
        {
            setImage("portal9.png");
        }
        if (enterTime == 1 || enterTime == 2)
        {
            setImage("portal10.png");
        }
    }
}
This actor is added into the world, by the world, after 1800 act cycles.
danpost danpost

2021/1/4

#
ItzLukeStorm wrote...
This actor is added into the world, by the world, after 1800 act cycles.
The same actor or a new Portal instance?
ItzLukeStorm ItzLukeStorm

2021/1/5

#
The same actor.
danpost danpost

2021/1/5

#
ItzLukeStorm wrote...
The same actor.
A new Portal instance would be better. Putting the same one in the world would end up immediately removing itself due to its field values.
ItzLukeStorm ItzLukeStorm

2021/1/5

#
I realized that, but that's not the problem. I changed it to this. }
if(enterTime == 21)
        {
            getWorld().removeObject(this);
        }
ItzLukeStorm ItzLukeStorm

2021/1/5

#
The actor stops turning at the moment of ( not necessarily because of ) enterTime == 2.
ItzLukeStorm ItzLukeStorm

2021/1/5

#
Meaning that the actor must be frozen in some sort.
danpost danpost

2021/1/5

#
ItzLukeStorm wrote...
The actor stops turning at the moment of ( not necessarily because of ) enterTime == 2.
That is your rotation method. enterTime is greater than 0 when enter is 2. You only have it rotating when enterTime is less than or equal to 0.
ItzLukeStorm ItzLukeStorm

2021/1/5

#
What I am trying to say is that enterTime doesn't keep counting upward, and I was wondering if someone could tell me why it is stopping at 2.
danpost danpost

2021/1/5

#
ItzLukeStorm wrote...
What I am trying to say is that enterTime doesn't keep counting upward, and I was wondering if someone could tell me why it is stopping at 2.
Change line 31 to:
if (allAboard == 0)
or
if (enter == 1 && allAboard <= 0)
ItzLukeStorm ItzLukeStorm

2021/1/5

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