Monster Racer Rush
Select between 5 monster racers, upgrade your monster skill and win the competition!
4.23 / 5.00 3,881 ViewsBuild and Base
Build most powerful forces, unleash hordes of monster and control your soldiers!
3.93 / 5.00 4,634 Viewshow 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!
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
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();
}
} 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!
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 ?
What is Sprites ?
A class that you should use instead of MovieClip.
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!
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?
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.I didn't understood second line >.>! can you give example?
Iterate over the array that holds all enemies instead. (do this in your Main class)
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
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 betterThis 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
At 11/27/13 09:10 AM, kkots wrote:At 11/27/13 07:10 AM, A-Genius wrote:Re-post your code with changes applied! :DAt 11/27/13 12:45 AM, milchreis wrote:
ok now I understood so I need to start with Sprite as my main class I will apply the changes then Re-post
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
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! :Dimport 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.
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
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
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.
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?
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.
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);
}
} 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.
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();
}
}
}
} 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.
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
At 11/28/13 05:51 PM, A-Genius wrote: why didn't you told me to create a main class!
kkots did
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.
I found the out why the object run slow! because I didn't remove my Enter frame listener
removeEventListener(Event.ENTER_FRAME,onEnter); 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?