Be a Supporter!

slow objects

  • 303 Views
  • 26 Replies
New Topic Respond to this Topic
A-Genius
A-Genius
  • Member since: Mar. 20, 2010
  • Offline.
Forum Stats
Member
Level 06
Blank Slate
slow objects 2013-11-26 23:53:07 Reply

how can I allow 20 object to move smoothly?

at the beginning my object begin to move, normally just after adding my second object it slowed down any way to speed up my code!

HappyWhaleStudios
HappyWhaleStudios
  • Member since: Feb. 1, 2013
  • Offline.
Forum Stats
Member
Level 07
Game Developer
Response to slow objects 2013-11-27 00:10:20 Reply

Can we see the code? Probably the issue is that you're not doing it in the most efficient way or your computer's just slow, but probably the first one, so post your code and I'll see if I have any suggestions on how to make it better


BBS Signature
A-Genius
A-Genius
  • Member since: Mar. 20, 2010
  • Offline.
Forum Stats
Member
Level 06
Blank Slate
Response to slow objects 2013-11-27 00:22:45 Reply

At 11/27/13 12:10 AM, HappyWhaleStudios wrote: Can we see the code? Probably the issue is that you're not doing it in the most efficient way or your computer's just slow, but probably the first one, so post your code and I'll see if I have any suggestions on how to make it better

This is the first code that I think contain the problem:

public class monster1 extends MovieClip
	{
		var oneTurn:Number = 0;
		var speed:Number = 1;
		public function monster1()
		{
			this.width = 49;
			this.height = 45;
			addEventListener(Event.ENTER_FRAME,MoveMonster);

		}

		function MoveMonster(e:Event):void
		{
			if (this.y != 300)
			{
				this.mapPattern();
			}
		}

public function mapPattern()
		{

			if (this.x < 168 && oneTurn == 0)
			{
				this.x +=  speed;
			}

			if (this.x == 168 && this.y > -103 && oneTurn == 0)
			{
				this.y -=  speed;
			}

			if (this.y == -103 && this.x < 435)
			{
				this.x +=  speed;
			}

			if (this.x == 435 && this.y < 125)
			{
				this.y +=  speed;
			}

			if (this.y == 125 && this.x > 150)
			{
				oneTurn = 1;
				this.x -=  speed;
			}

			if (this.x == 150 && this.y < 300)
			{
				this.y +=  speed;
			}

			if (this.x == 150 && this.y == 300)
			{
				this.destory();
			}

		}
milchreis
milchreis
  • Member since: Jan. 11, 2008
  • Offline.
Forum Stats
Member
Level 26
Programmer
Response to slow objects 2013-11-27 00:45:55 Reply

Do not add an ENTER_FRAME listener to every object.
Iterate over the array that holds all enemies instead. (do this in your Main class)

Use Sprite instead of MovieClip as a super class.

What is "destory()" ?
Do you mean destroy?
Post that function!

A-Genius
A-Genius
  • Member since: Mar. 20, 2010
  • Offline.
Forum Stats
Member
Level 06
Blank Slate
Response to slow objects 2013-11-27 00:50:42 Reply

At 11/27/13 12:45 AM, milchreis wrote: Do not add an ENTER_FRAME listener to every object.
Iterate over the array that holds all enemies instead. (do this in your Main class)

Use Sprite instead of MovieClip as a super class.

What is "destory()" ?
Do you mean destroy?
Post that function!

What is Sprites ?

milchreis
milchreis
  • Member since: Jan. 11, 2008
  • Offline.
Forum Stats
Member
Level 26
Programmer
Response to slow objects 2013-11-27 01:39:42 Reply

What is Sprites ?

A class that you should use instead of MovieClip.

A-Genius
A-Genius
  • Member since: Mar. 20, 2010
  • Offline.
Forum Stats
Member
Level 06
Blank Slate
Response to slow objects 2013-11-27 07:04:19 Reply

At 11/27/13 01:39 AM, milchreis wrote:
What is Sprites ?
A class that you should use instead of MovieClip.

but I don't draw a sprites using codes! I already have ready MovieClip!

A-Genius
A-Genius
  • Member since: Mar. 20, 2010
  • Offline.
Forum Stats
Member
Level 06
Blank Slate
Response to slow objects 2013-11-27 07:10:44 Reply

At 11/27/13 12:45 AM, milchreis wrote: Do not add an ENTER_FRAME listener to every object.
Iterate over the array that holds all enemies instead. (do this in your Main class)

I didn't understood second line >.>! can you give example?

kkots
kkots
  • Member since: Apr. 16, 2013
  • Offline.
Forum Stats
Supporter
Level 10
Blank Slate
Response to slow objects 2013-11-27 09:10:10 Reply

At 11/27/13 07:10 AM, A-Genius wrote:
At 11/27/13 12:45 AM, milchreis wrote: Do not add an ENTER_FRAME listener to every object.
Iterate over the array that holds all enemies instead. (do this in your Main class)
I didn't understood second line >.>! can you give example?

OK.

//do not copy-paste this code or use it in any way
//this is supposed to be only read and understood
package com.newgrounds.programmerAmbulance{
  import flash.events.Event;

  public class Main extends Sprite{ //this "extends Sprite" is what milchreis was talking about
    public var enemies:Array; //this is an array where you put all enemies
    public function Main(){
      //this is where the game is created
      addEventListener(Event.ADDED_TO_STAGE,init);
      //won't do anything here though
    }
    function init(e:Event):void{
      //this is where the game appears on stage
      removeEventListener(Event.ADDED_TO_STAGE,init); //removing the event, so that init runs only once per game
      enemies=[]; //create a new, empty enemies array
      addEventListener(Event.ENTER_FRAME,oef); //make ONE enter-frame listener
    }
    function oef(e:Event):void{
      //this is your main enter-frame function, the only enter-frame in the whole game
      for(var i:uint=0; i<enemies.length; i++){
        enemies[i].moveMonster(e);
      }
    }
  }
}
package com.newgrounds.programmerAmbulance{
  public class monster1 extends MovieClip{
		var oneTurn:Number = 0;
		var speed:Number = 1;
		public function monster1()
		{
			this.width = 49;
			this.height = 45;
			//addEventListener(Event.ENTER_FRAME,MoveMonster);
			//NONONO DON'T DO THIS!!! NOOOO!!!
			//MAIN WILL CALL MOVEMONSTER FOR YOU

		}

		function MoveMonster(e:Event):void
		{
			if (this.y != 300)
			{
				this.mapPattern();
			}
		}

Re-post your code with changes applied! :D


BBS Signature
graives
graives
  • Member since: Nov. 26, 2013
  • Offline.
Forum Stats
Member
Level 07
Game Developer
Response to slow objects 2013-11-27 09:10:55 Reply

At 11/27/13 12:22 AM, A-Genius wrote:
At 11/27/13 12:10 AM, HappyWhaleStudios wrote: Can we see the code? Probably the issue is that you're not doing it in the most efficient way or your computer's just slow, but probably the first one, so post your code and I'll see if I have any suggestions on how to make it better
This is the first code that I think contain the problem:

When you add your objects you are going to want to be adding them to an array. After you do that, you can access the objects by using a for loop. This will prevent you from having a bunch of the same functions running and condense it nicely into one single loop.

To make a for loop you will want to do something like:

for(var i:int = 0; i < arrayName.length; i++)
{
      stuff here
}

To tell the monsters what to do you will be using arrayName[i] instead of "this". So this.x would become arrayName[i].x

A-Genius
A-Genius
  • Member since: Mar. 20, 2010
  • Offline.
Forum Stats
Member
Level 06
Blank Slate
Response to slow objects 2013-11-27 13:17:31 Reply

At 11/27/13 09:10 AM, kkots wrote:
At 11/27/13 07:10 AM, A-Genius wrote:
At 11/27/13 12:45 AM, milchreis wrote:
Re-post your code with changes applied! :D

ok now I understood so I need to start with Sprite as my main class I will apply the changes then Re-post

A-Genius
A-Genius
  • Member since: Mar. 20, 2010
  • Offline.
Forum Stats
Member
Level 06
Blank Slate
Response to slow objects 2013-11-27 15:05:18 Reply

At 11/27/13 09:10 AM, kkots wrote: Re-post your code with changes applied! :D
import flash.display.Sprite;
	import flash.events.*;

	public class monster1 extends Sprite
	{
		public var myEnemy:Array = []; // add my objects inside my array
		var oneTurn:Number = 0;
		var speed:Number = 1;
		
		public function monster1()
		{
			this.width = 49;
			this.height = 45;
			addEventListener(Event.ADDED_TO_STAGE,Launch); //when the object is add to stage
			trace(myEnemy.length);
		}
		
		function Launch(e:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE,Launch); //remove listener to work for one object
			myEnemy=[this];
			addEventListener(Event.ENTER_FRAME,MyEnterFrame); // create listener to add into frame
			
		}
		
		function MyEnterFrame(e:Event):void
		{
				for(var i:uint = 0; i<myEnemy.length; i++){
				myEnemy[i].moveMonster(e);
			}
		}
		
		function moveMonster(e:Event):void
		{
			if (this.y != 300)
			{
				this.mapPattern();
			}
		}

it still act slow >.> when I add 20 monster

graives
graives
  • Member since: Nov. 26, 2013
  • Offline.
Forum Stats
Member
Level 07
Game Developer
Response to slow objects 2013-11-27 15:43:13 Reply

At 11/27/13 03:05 PM, A-Genius wrote:
At 11/27/13 09:10 AM, kkots wrote: Re-post your code with changes applied! :D
import flash.display.Sprite;
import flash.events.*;

public class monster1 extends Sprite
{
public var myEnemy:Array = []; // add my objects inside my array
var oneTurn:Number = 0;
var speed:Number = 1;

public function monster1()
{
this.width = 49;
this.height = 45;
addEventListener(Event.ADDED_TO_STAGE,Launch); //when the object is add to stage
trace(myEnemy.length);
}

function Launch(e:Event):void
{
removeEventListener(Event.ADDED_TO_STAGE,Launch); //remove listener to work for one object
myEnemy=[this];
addEventListener(Event.ENTER_FRAME,MyEnterFrame); // create listener to add into frame

}

function MyEnterFrame(e:Event):void
{
for(var i:uint = 0; i<myEnemy.length; i++){
myEnemy[i].moveMonster(e);
}
}

function moveMonster(e:Event):void
{
if (this.y != 300)
{
this.mapPattern();
}
}

it still act slow >.> when I add 20 monster
function MyEnterFrame(e:Event):void
		{
				for(var i:uint = 0; i<myEnemy.length; i++){
				myEnemy[i].moveMonster(e);
			}
		}

This needs to go outside of the monster1 class (along with the myEnemy array). Also, make sure moveMonster is set to public.

A-Genius
A-Genius
  • Member since: Mar. 20, 2010
  • Offline.
Forum Stats
Member
Level 06
Blank Slate
Response to slow objects 2013-11-27 15:54:59 Reply

At 11/27/13 03:43 PM, graives wrote: This needs to go outside of the monster1 class (along with the myEnemy array). Also, make sure moveMonster is set to public.
public class monster1 extends Sprite
	{
		public var myEnemy:Array = []; // add my objects inside my array
		
		function MyEnterFrame(e:Event):void
		{
				for(var i:uint = 0; i<myEnemy.length; i++){
				myEnemy[i].moveMonster(e);
			}
		}


public function moveMonster(e:Event):void
		{
			if (this.y != 300)
			{
				this.mapPattern();
			}
		}

ok

A-Genius
A-Genius
  • Member since: Mar. 20, 2010
  • Offline.
Forum Stats
Member
Level 06
Blank Slate
Response to slow objects 2013-11-27 15:56:30 Reply

At 11/27/13 03:43 PM, graives wrote: This needs to go outside of the monster1 class (along with the myEnemy array). Also, make sure moveMonster is set to public.

still slow

graives
graives
  • Member since: Nov. 26, 2013
  • Offline.
Forum Stats
Member
Level 07
Game Developer
Response to slow objects 2013-11-27 15:58:17 Reply

At 11/27/13 03:54 PM, A-Genius wrote:
At 11/27/13 03:43 PM, graives wrote: This needs to go outside of the monster1 class (along with the myEnemy array). Also, make sure moveMonster is set to public.
public class monster1 extends Sprite
{
public var myEnemy:Array = []; // add my objects inside my array

function MyEnterFrame(e:Event):void
{
for(var i:uint = 0; i<myEnemy.length; i++){
myEnemy[i].moveMonster(e);
}
}


public function moveMonster(e:Event):void
{
if (this.y != 300)
{
this.mapPattern();
}
}

ok

Nah, your loop has to go in a completely different class. Put it in the class that is calling your monster1.

A-Genius
A-Genius
  • Member since: Mar. 20, 2010
  • Offline.
Forum Stats
Member
Level 06
Blank Slate
Response to slow objects 2013-11-27 16:03:18 Reply

At 11/27/13 03:58 PM, graives wrote: Nah, your loop has to go in a completely different class. Put it in the class that is calling your monster1.

ok you mean I should create a new public class right?

milchreis
milchreis
  • Member since: Jan. 11, 2008
  • Offline.
Forum Stats
Member
Level 26
Programmer
Response to slow objects 2013-11-27 16:06:55 Reply

At 11/27/13 04:03 PM, A-Genius wrote: ok you mean I should create a new public class right?

No.
You should put that code into the class that creates the monsters, which is you main class.

A-Genius
A-Genius
  • Member since: Mar. 20, 2010
  • Offline.
Forum Stats
Member
Level 06
Blank Slate
Response to slow objects 2013-11-27 16:25:45 Reply

At 11/27/13 04:06 PM, milchreis wrote:
At 11/27/13 04:03 PM, A-Genius wrote: ok you mean I should create a new public class right?
No.
You should put that code into the class that creates the monsters, which is you main class.

I already add the code in my main

public class monster1 extends Sprite
	{
		public var myEnemy:Array = []; // add my objects inside my array
		
		function MyEnterFrame(e:Event):void
		{
				for(var i:uint = 0; i<myEnemy.length; i++){
				myEnemy[i].moveMonster(e);
			}
		}
milchreis
milchreis
  • Member since: Jan. 11, 2008
  • Offline.
Forum Stats
Member
Level 26
Programmer
Response to slow objects 2013-11-27 16:52:21 Reply

At 11/27/13 04:25 PM, A-Genius wrote: I already add the code in my main

public class monster1 extends Sprite
{
public var myEnemy:Array = []; // add my objects inside my array

function MyEnterFrame(e:Event):void
{
for(var i:uint = 0; i<myEnemy.length; i++){
myEnemy[i].moveMonster(e);
}
}

No, you didn't.
Take a look at kots' code again, there's a class "Main" and a class "monster1"
Now class should always start with a capital letter and should never end with a useless number.
So rename your class to "Monster".
Then add the code that loops through the array to your "Main" class.

Try again.

A-Genius
A-Genius
  • Member since: Mar. 20, 2010
  • Offline.
Forum Stats
Member
Level 06
Blank Slate
Response to slow objects 2013-11-27 17:29:06 Reply

At 11/27/13 04:52 PM, milchreis wrote: No, you didn't.
Take a look at kots' code again, there's a class "Main" and a class "monster1"
Now class should always start with a capital letter and should never end with a useless number.
So rename your class to "Monster".
Then add the code that loops through the array to your "Main" class.

Try again.

I am using monster_1 because I want to create the same class for another monster inside my library

package  {
	
	import flash.display.MovieClip;
	import flash.events.*;
	
	public class monster_1 extends MovieClip {
		
		public var myEnemy:Array = []; // add my objects inside my array
		var oneTurn:Number = 0;
		var speed:Number = 1;

		public function monster_1() {
			this.width = 49;
			this.height = 45;
			
			addEventListener(Event.ADDED_TO_STAGE,Launch); //when the object is add to stage
			
			addEventListener(Event.ENTER_FRAME,MyEnterFrame); // create listener to add into frame
			
			function MyEnterFrame(e:Event):void
		
			{
				for(var i:uint = 0; i<myEnemy.length; i++){
				myEnemy[i].moveMonster(e);
														  }
		     }
		}
		
		
		
	 public function Launch(e:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE,Launch); //remove listener to work for one object
			myEnemy=[this];
			
			
		}
		
		public function moveMonster(e:Event):void
		{
			if (this.y != 300)
			{
				this.mapPattern();
			}
		}
		

		public function destory()
		{
			this.parent.removeChild(this);
		}



		public function mapPattern()
		{

			if (this.x < 168 && oneTurn == 0)
			{
				this.x +=  speed;
			}

			if (this.x == 168 && this.y > -103 && oneTurn == 0)
			{
				this.y -=  speed;
			}

			if (this.y == -103 && this.x < 435)
			{
				this.x +=  speed;
			}

			if (this.x == 435 && this.y < 125)
			{
				this.y +=  speed;
			}

			if (this.y == 125 && this.x > 150)
			{
				oneTurn = 1;
				this.x -=  speed;
			}

			if (this.x == 150 && this.y < 300)
			{
				this.y +=  speed;
			}

			if (this.x == 150 && this.y == 300)
			{
				this.destory();
			}



		}

	}

}
milchreis
milchreis
  • Member since: Jan. 11, 2008
  • Offline.
Forum Stats
Member
Level 26
Programmer
Response to slow objects 2013-11-27 17:35:51 Reply

You've been told several times to move the code into your Main class which is _not_ monster.
If you don't want help, don't ask for it, ok?

At 11/27/13 05:29 PM, A-Genius wrote: I am using monster_1 because I want to create the same class for another monster inside my library

You still call it Monster and use it as a super class.
But you're not there yet, because you do not listen to the advices you asked for.
Give your classes meaningful names.

Look at the code examples again.
Read all posts again carefully.
Try again.

A-Genius
A-Genius
  • Member since: Mar. 20, 2010
  • Offline.
Forum Stats
Member
Level 06
Blank Slate
Response to slow objects 2013-11-28 17:51:16 Reply

At 11/27/13 05:35 PM, milchreis wrote: You've been told several times to move the code into your Main class which is _not_ monster.

why didn't you told me to create a main class! I haven't created my main class yet! I was doing my codes inside my exported actionscript file inside my object! :P

milchreis
milchreis
  • Member since: Jan. 11, 2008
  • Offline.
Forum Stats
Member
Level 26
Programmer
Response to slow objects 2013-11-28 20:41:54 Reply

At 11/28/13 05:51 PM, A-Genius wrote: why didn't you told me to create a main class!

kkots did

kkots
kkots
  • Member since: Apr. 16, 2013
  • Offline.
Forum Stats
Supporter
Level 10
Blank Slate
Response to slow objects 2013-11-28 22:00:25 Reply

At 11/28/13 08:41 PM, milchreis wrote:
At 11/28/13 05:51 PM, A-Genius wrote: why didn't you told me to create a main class!
kkots did

I assume you didn't read and understand my code? I have no idea how to help a person who does not want help, yet asks for help.


BBS Signature
A-Genius
A-Genius
  • Member since: Mar. 20, 2010
  • Offline.
Forum Stats
Member
Level 06
Blank Slate
Response to slow objects 2013-12-06 15:09:23 Reply

I found the out why the object run slow! because I didn't remove my Enter frame listener

removeEventListener(Event.ENTER_FRAME,onEnter);
kkots
kkots
  • Member since: Apr. 16, 2013
  • Offline.
Forum Stats
Supporter
Level 10
Blank Slate
Response to slow objects 2013-12-06 17:20:33 Reply

At 12/6/13 03:09 PM, A-Genius wrote: I found the out why the object run slow! because I didn't remove my Enter frame listener

removeEventListener(Event.ENTER_FRAME,onEnter);

What is onEnter function? You've never mentioned it before. Is it something new that you added?


BBS Signature