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

2015/9/13

Problem with each-for

Vellyxenya Vellyxenya

2015/9/13

#
Hi, here's what I want to do: Some Devices spawn from time to time in the world, and at a moment, they all stop and they aim at the ship I control by creating a series of little points (called DeviceLaser) going from the Devices to my ship. I succeeded in doing that, but the problem is that this "laser of points" only spawn from the last Device that have been created and I want that Every Device create a "laser of points" going from it to my ship (sorry for the repetitions). Here's the code in my World:
public void act()
    {
        createMonsters();
        createDevices();
        Options();
        changeGround();
        field();
        Counter++;
        beReady();
        aim();
    }

public void beReady()
    {
        if(Game.Counter>500 && Game.Counter%1000 == 0)
        {
            ready = true;
        }
    }

    public void aim()
    {
        if(ready == true)
        {
            List<Devices> devices = getObjects(Devices.class);
            for(Devices device: devices)
            {
                Actor gear3 = (Actor)getObjects(Gear3.class).get(0);
                int xGear = gear3.getX();
                int yGear = gear3.getY();
                int xDevice = device.getX();
                int yDevice = device.getY();
                int dx = xGear-xDevice;
                int dy = yGear-yDevice;
                double length = Math.sqrt(dx*dx+dy*dy);
                double xLength = dx/length;
                double yLength = dy/length;

                List<DeviceLaser> lasers = getObjects(DeviceLaser.class);
                removeObjects(lasers);
                for(int i=0; i<length; i++)
                {
                    DeviceLaser devicelaser = new DeviceLaser();
                    addObject(devicelaser, (int)(device.getX()+i*xLength), (int)(device.getExactY()+i*yLength));
                }
            }
        }
    }
Thanks :)
Super_Hippo Super_Hippo

2015/9/13

#
I think the problem is in lines 39 and 40. Every Device removes all DeviceLasers from the world (39+40) before they add their own DeviceLasers (43+44). So in the end, only the last device has its lasers.
Vellyxenya Vellyxenya

2015/9/13

#
Yes I tried to remove the line 39-40 from the code and all the lines spawn from all the Devices!! The problem now is that when i move my ship, the laseres doesn't move correctly: here's what it shows^^ link
Vellyxenya Vellyxenya

2015/9/13

#
Oh yes, I fixed that by placing the code that was going wrong elsewhere like this:
public void aim()
    {
        if(ready == true)
        {
            List<DeviceLaser> lasers = getObjects(DeviceLaser.class);
            removeObjects(lasers);
            List<Devices> devices = getObjects(Devices.class);
            for(Devices device: devices)
            {
                Actor gear3 = (Actor)getObjects(Gear3.class).get(0);
                int xGear = gear3.getX();
                int yGear = gear3.getY();
                int xDevice = device.getX();
                int yDevice = device.getY();
                int dx = xGear-xDevice;
                int dy = yGear-yDevice;
                double length = Math.sqrt(dx*dx+dy*dy);
                double xLength = dx/length;
                double yLength = dy/length;

                
                for(int i=0; i<length; i++)
                {
                    DeviceLaser devicelaser = new DeviceLaser();
                    addObject(devicelaser, (int)(device.getX()+i*xLength), (int)(device.getExactY()+i*yLength));
                }
            }
        }
    }
Thank you very much!!! :D
Super_Hippo Super_Hippo

2015/9/13

#
You're welcome ;) You can also put lines 10-12 outside the for loop.
Vellyxenya Vellyxenya

2015/9/13

#
Thanks! By the way, my scenario is lagging... since some days ago when I added some weapons and the healthbars etc... Any idea how to fix this? It works correctly for like 10-15 seconds and then everything is slowed down, especially when I fire... (My computer can afford a lot of "heavy" games with high graphics)
Super_Hippo Super_Hippo

2015/9/13

#
You could add this line in the act method of your world for testing:
System.out.println(numberOfObjects());
If it shows big numbers or numbers that increase over time, there are maybe too many objects in the world or objects that aren't needed anymore aren't removed. If not, you could try to remove unused code.
Vellyxenya Vellyxenya

2015/9/13

#
yes true, they are increasing, within a few seconds it reachs 3000, is there a way to know what is causing all this mess?^^
Vellyxenya Vellyxenya

2015/9/13

#
It's okay I fixed it, I had a Lifebar that was created every act without being removed^^
danpost danpost

2015/9/13

#
Vellyxenya wrote...
It's okay I fixed it, I had a Lifebar that was created every act without being removed^^
I hope you did not fix it by having one Lifebar replacing another (by adding a new one and removing the old one) every act cycle. You should have one Lifebar stay in the world and have its image change only when the number of lives changes.
You need to login to post a reply.