Home Tutorials Forums Articles Blogs Movies Library Employment Press

Go Back   ActionScript.org Forums > General > Gaming and Game Development

Reply
 
Thread Tools Rate Thread Display Modes
Old 04-23-2012, 08:26 AM   #1
jpoopdog
Member
 
Join Date: Feb 2012
Posts: 52
Default [AS3] My collision system is faulty and is lagging my game

hello,

After little help with my previous thread asking for a possible solution to my problem, being that i had no idea how to do X and Y collisions, i have finally gotten it to work, to an extent.

I got X and Y collisions, to work, but it is very strange, only X collisions work smoothly, or at least they did before i moved it all out of my zombie class and onto the main game class since i was told that the enemies shouldnt be doing their own collisions.

Anyway, i got it all working now, in a way, but now the game laggs up really bad when theres over 5 zombies on screen, doing collisions, which then causes some to glitch and get stuck in walls, which in turn worsens the lagg.


can someone please help? what do i need to do to get this to work without lagging so bad, and perhaps also actually get both x and y collisions to work, instead of just the one which is called first in the for loop (which is generally X).

in the game, wherever the mouse is clicked a zombie is spawned, as a block.
Attached Files
File Type: zip boxcraft.zip (132.5 KB, 41 views)
jpoopdog is offline   Reply With Quote
Old 04-23-2012, 03:09 PM   #2
abeall
Senior Member
 
Join Date: Feb 2006
Location: Washington, DC
Posts: 2,796
Send a message via AIM to abeall
Default

Hi there,

You're much more likely to get help if you can post code, not many people will download a zip right off the bat. I realize you might have a lot of code and you aren't sure where the problems stem from, but try to isolate your problem areas and post the code from there.
__________________
Aaron Beall | Flash portfolio | Fireworks extensions | Twitter
abeall is offline   Reply With Quote
Old 04-27-2012, 04:12 AM   #3
jpoopdog
Member
 
Join Date: Feb 2012
Posts: 52
Default

I may have changed some things around, but heres the code for the zombie class.
ActionScript Code:
package {     import flash.display.MovieClip;     import flash.display.DisplayObject;     import flash.events.*;     public class Zombie extends MovieClip {         private var lacc:Number=0;         private var racc:Number=0;         private var uacc:Number=0;         private var dacc:Number=0;         public var LastX:Number=50;         public var LastY:Number=50;         private var timer:Number=1;         private var timer2:Number=1;         private var loffset:Number=100;         private var roffset:Number=100;         private var uoffset:Number=100;         private var doffset:Number=100;         private var ran1:Number=1;         private var ran2:Number=1;         public var Xhit:Boolean=false;         public var Yhit:Boolean=false;         public var dx:Number=0;         public var dy:Number=0;         public var bump:Boolean=false;         private var stuck:Number=0;         private var mL:Boolean=false;         private var mR:Boolean=false;         private var mU:Boolean=false;         private var mD:Boolean=false;         public var doomtime:Number=0;         //public var hit:Boolean=false;         public function Zombie() {             x=mouseX;             y=mouseY;         }         public function moveDownABit():void {             timer2+=1;             if (timer2>=ran2) {                 timer2=0;                 dx=racc*roffset/75-lacc*loffset/75;                 dy=uacc*uoffset/75-dacc*doffset/75;                 ran2=Math.random()*10;             }             if (doomtime==0) {                 if (Xhit==false) {                     LastX=x;                     x+=dx;                 } else {                     x-=dx;                     //dx=0;                 }                 if (Yhit==false) {                     LastY=y;                     y-=dy;                 } else {                     //y-=dy;                     dy=0;                 }             } else {                 doomtime+=1;             }             /*MovieClip(root).moveleft.text=mL;             MovieClip(root).moveright.text=mR;             MovieClip(root).moveup.text=mU;             MovieClip(root).movedown.text=mD;             MovieClip(root).ltb.text=lacc;             MovieClip(root).rtb.text=racc;             MovieClip(root).utb.text=uacc;             MovieClip(root).dtb.text=dacc; */             timer+=1;             if (timer>=ran1) {                 loffset=Math.random()*100;                 roffset=Math.random()*100;                 uoffset=Math.random()*100;                 doffset=Math.random()*100;                 ran1=Math.random()*150;                 timer=0;             }             if (x<MovieClip(root).targX) {                 mR=true;                 mL=false;                 //if player is to the right                 if (racc<=4) {                     racc=racc+0.2;                 }                 if (lacc>=0) {                     lacc=lacc-0.2;                 }             }             if (x>MovieClip(root).targX) {                 mL=true;                 mR=false;                 //if player is to the left                 if (lacc<=4) {                     lacc=lacc+0.2;                 }                 if (racc>=0) {                     racc=racc-0.2;                 }             }             //}             if (y<MovieClip(root).targY) {                 mD=true;                 mU=false;                 //if player is beneath                 if (dacc<=4) {                     //if (lastpress == 1)                     dacc=dacc+0.2;                 }                 if (uacc>=0) {                     uacc=uacc-0.2;                 }             }             if (y>MovieClip(root).targY) {                 mU=true;                 mD=false;                 //if player is above                 if (uacc<=4) {                     uacc=uacc+0.2;                 }                 if (dacc>=0) {                     dacc=dacc-0.2;                 }             }         }         //private function moveX():void {         //if (Yhit==false) {         /*         for (var a in MovieClip(root).dirt) {         Xhit=false;                 if (MovieClip(root).dirt[a].hitTestObject(this)==true) {                 Xhit=true;         collideX();                 }         }         //}                 */         //}         //public function collideX():void {         //trace("x is last x");         //x=LastX;         //racc=0;         //lacc=0;         //}         //private function moveY():void {         //if (Xhit==false) {         /*Yhit=false;         for (var a in MovieClip(root).dirt) {                 if (MovieClip(root).dirt[a].hitTestObject(this)==true) {         Yhit=true;                 collideY();         }                                 }         //}         */         //}         //public function collideY():void {         //if(Xhit==false){         //uacc=0;         //dacc=0;         //y=LastY;         //}         //}     } }
jpoopdog is offline   Reply With Quote
Old 04-27-2012, 05:00 AM   #4
rrh
throw a trace() in there
 
Join Date: Dec 2006
Posts: 1,982
Default

So is your collision testing all in your commented-out setions?
Code:
   for (var a in MovieClip(root).dirt) {
        Xhit=false;
        
        if (MovieClip(root).dirt[a].hitTestObject(this)==true) {
        
          Xhit=true;
          collideX();
        
        }
   }
How many elements do you expect to be in "dirt?" Is it just an Array or what?

Assuming it's an Array...
Code:
 var dirt:Array = MovieClip(root).dirt;
 Xhit=false;
 for (var a in dirt) {       
        if (MovieClip(dirt[a]).hitTestObject(this)==true) {
        
          Xhit=true;
          collideX();
          break;
        }
 }
Three changes:
1 set "dirt" before the loop, so I didn't need to keep re-casting "root"
2 set Xhit to false before the loop, so it only does it the once. Before, if the last entry of dirt doesn't collide, then Xhit would return to false for the last go through the loop, even if an earlier entry collided
3 I added the break, since once it's found one collision it doesn't seem to matter if it finds two


I also question the value of testing for Xhit inside the collideY function. What if it hits a corner, where moving just the X position back won't be enough to get it out of the wall? Then it might get stuck.
rrh is offline   Reply With Quote
Old 05-01-2012, 12:21 AM   #5
jpoopdog
Member
 
Join Date: Feb 2012
Posts: 52
Default

hi, i added your changes to the game, and performance wise it helped quoite a bit.
ive solved the collisions problem though, for seperately detecting y,x collisions, by using this code within the doom time if statement, instead of whats there now.

ActionScript Code:
LastX=x;//Set the last x position without being in touch with a dirt tile, to current x                 x+=dx;// Increase x by x acceleration                 moveX();// check to see if there was a collision after moving.                 LastY=y;// Set the last x position without being in touch with a dirt tile, to current x                 y-=dy;// Increase x by x acceleration                 moveY();// check to see if there was a collision after moving.

this way collisions are finally clean. also i moved collisions back into the zombie class since it didnt actually improve performance to have it in the main game AS.

would you perhaps know though how i could better improve performance?
would turning zombies into sprites for instance help?

edit--

also, now i AM using the commented out code at the bottom you have asked about. i had Xhit inside the for loop before because otherwise collisions would work at all.
jpoopdog is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 06:51 AM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.