Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 04-11-2010, 12:30 AM   #1
Parrot_King
Registered User
 
Join Date: Apr 2010
Posts: 6
Question [AS3] Game and Watch Help

Hey everyone, Iím new to this forum and actionscript in general. For a class I have we are required to make a Game and Watch style game, now because of the games limitations everything is calculated using key frames. I have all the basic game code done, but it just returns errors. I have tried isolating the code to no effect...
If someone could please look at my file and tell me what Iím doing wrong I would really appreciates it... Iíd be happy to email you the script and basic game...
Thanks hope someone will help me on this; Iíve been stuck for days!!!
Parrot_King is offline   Reply With Quote
Old 04-12-2010, 01:58 AM   #2
UnknownJoe796
AS3 Game Programmer
 
Join Date: Nov 2009
Posts: 58
Default

I highly suggest first bringing your problems to someone local - learning to debug your own code is a critical part of programming, and that is something that just isn't easily taught over the internet.
However, I'll help you out if you want me to - just give me a link instead of email. I don't like to give my email away on websites that can be indexed by Google.
UnknownJoe796 is offline   Reply With Quote
Old 04-13-2010, 03:20 PM   #3
rrh
throw a trace() in there
 
Join Date: Dec 2006
Posts: 1,982
Default

If it's returning errors, read what the errors are trying to tell you. And if you ask for help on a forum, post what the errors say, and maybe someone will recognize the problem.
rrh is offline   Reply With Quote
Old 04-14-2010, 02:36 AM   #4
Parrot_King
Registered User
 
Join Date: Apr 2010
Posts: 6
Default [AS3] Game and Watch- Stop spawning enemy

hi everyone im new to action script and am making a game for a class based on game and watch.

now I have created an array that spawns enemys if there are less than a number on the stage, it then moves them along the keyframes in the classes movie clip. What im having trouble with is when you hit a enemy (determined by there keyframe) it removes the array, but I only want to remove the 'hit' child from the array, instead it removes all arrays from the stage.

Here is my code for the function to remove the arrays.

ActionScript Code:
function removeEnemy():void {     var f:int;             //remove all enemys         for (f=farmerArray.length-1; f>=0; f--) {                             removeChild(farmerArray[f]);             farmerArray.splice(f,1);             }     for (f=samuraiArray.length-1; f>=0; f--) {                     removeChild(samuraiArray[f]);             samuraiArray.splice(f,1);             }     for (f=ninjaArray.length-1; f>=0; f--) {                     removeChild(ninjaArray[f]);             ninjaArray.splice(f,1);         }     for (f=fatArray.length-1; f>=0; f--) {                     removeChild(fatArray[f]);             fatArray.splice(f,1);             }     for (f=ninja2Array.length-1; f>=0; f--) {         removeChild(ninja2Array[f]);             ninja2Array.splice(f,1);             } trace("Remove") }

do I need an if statement?

also when the enemy is sparning because of its chance of spawning will spawn multiple enemys ontop of eachother, once the enemy is spawned it plays the first few frames of the movie clip(you see the char flash a couple times then start moving) how can i stop the enemy from spawning again so it dose not spawn ontop of the last enemy spawned.

Do i need a timer after the spawn or something like that???

Here is the enemy spawn code

ActionScript Code:
function spawnEnemy():void {     var chance:int=Math.floor(Math.random()*8);     var newEnemy:MovieClip;     if (chance==1) {         var whichEnemy:int=Math.floor(Math.random()*6);// adjust the 1 for the number of different enemies         switch (whichEnemy) {             case 0 :                 if (farmerArray.length<7) {                     newEnemy = new FarmerEnemy();                     newEnemy.x=593;                     newEnemy.y=242;                     farmerArray.push(newEnemy);                     //trace (farmerArray.length);                     // Add the enemy to the visible stage                     addChild(newEnemy);                 }                 if (farmerArray.length==7) {                     trace("Game Over");                     charDied();                 }             case 1 :                 if (ninjaArray.length<7) {                     newEnemy = new NinjaEnemy();                     newEnemy.x=-158.7;                     newEnemy.y=381.3;                     ninjaArray.push(newEnemy);                     //trace(ninjaArray.length);                     // Add the enemy to the visible stage                     addChild(newEnemy);                 }                 if (fatArray.length==7) {                     trace("charDied();");                     charDied();                 }                 if (chance==1) {                 }             case 2 :                 if (fatArray.length<7) {                     newEnemy = new FatEnemy();                     newEnemy.x=-357.3;                     newEnemy.y=339.1;                     fatArray.push(newEnemy);                     //trace(fatArray.length);                     // Add the enemy to the visible stage                     addChild(newEnemy);                 }                 if (fatArray.length==7) {                     trace("charDied();");                     charDied();                 }             case 3 :                 if (samuraiArray.length<6) {                     newEnemy = new SamuraiEnemy();                     newEnemy.x=-312.7;                     newEnemy.y=110.1;                     samuraiArray.push(newEnemy);                     //trace(samuraiArray.length);                     // Add the enemy to the visible stage                     addChild(newEnemy);                 }                 if (samuraiArray.length==6) {                     trace("charDied();");                     charDied();                 }             case 4 :                 if (ninja2Array.length<8) {                     newEnemy = new Ninja2Enemy();                     newEnemy.x=-213.2;                     newEnemy.y=76.2;                     ninja2Array.push(newEnemy);                     //trace(ninja2Array.length);                     // Add the enemy to the visible stage                     addChild(newEnemy);                 }                 if (ninja2Array.length==8) {                     trace("charDied();");                     charDied();                 }         }         // case 1:// other type of enemy etc.     } } // === MOVE ALL ENEMIES function moveEnemy() {     var c:int;     for (c = farmerArray.length - 1; c >=0; c--) {         if (farmerArray[c].currentFrame<farmerArray[c].totalFrames-c) {             farmerArray[c].gotoAndStop(farmerArray[c].currentFrame + 1);         }     }     for (c = ninjaArray.length - 1; c >=0; c--) {         if (ninjaArray[c].currentFrame<ninjaArray[c].totalFrames-c) {             ninjaArray[c].gotoAndStop(ninjaArray[c].currentFrame + 1);         }     }     for (c = fatArray.length - 1; c >=0; c--) {         if (fatArray[c].currentFrame<fatArray[c].totalFrames-c) {             fatArray[c].gotoAndStop(fatArray[c].currentFrame + 1);         }     }     for (c = samuraiArray.length - 1; c >=0; c--) {         if (samuraiArray[c].currentFrame<samuraiArray[c].totalFrames-c) {             samuraiArray[c].gotoAndStop(samuraiArray[c].currentFrame + 1);                     }     }     for (c = ninja2Array.length - 1; c >=0; c--) {         if (ninja2Array[c].currentFrame<ninja2Array[c].totalFrames-c) {             ninja2Array[c].gotoAndStop(ninja2Array[c].currentFrame + 1);                     }     }     if (checkEnemyHit()) {         if (isAttacking) {             getPoints(10);         }     }

If anyone could help I would really appreciate it!!!!
Parrot_King is offline   Reply With Quote
Old 04-14-2010, 02:44 AM   #5
Artofwork
Senior Member
 
Join Date: Feb 2010
Posts: 180
Default

try

ActionScript Code:
samuraiArray.removeChildAt(f)
__________________
Scientific method consists of the collection of data through observation and experimentation, and the formulation and testing of hypotheses.
Artofwork is offline   Reply With Quote
Old 04-14-2010, 03:00 AM   #6
Parrot_King
Registered User
 
Join Date: Apr 2010
Posts: 6
Default tried

i get a type error for that code...

TypeError: Error #1006: removeChildAt is not a function.
at WorkingThunderKing_004_fla::MainTimeline/removeEnemy()
at WorkingThunderKing_004_fla::MainTimeline/addAPoint()
at flash.events::EventDispatcher/dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at flash.utils::Timer/tick()
Parrot_King is offline   Reply With Quote
Old 04-14-2010, 03:34 AM   #7
UnknownJoe796
AS3 Game Programmer
 
Join Date: Nov 2009
Posts: 58
Default

Problem with your removal code is simple to fix. Replace your removal code for each enemy type with something like this:
ActionScript Code:
for each(var enemy in enemyArray) {    removeChild(enemy); } enemyArray=new Array();
For each statements are one of the niftiest things to come about in a long time - use them. Your code does not work because you are removing them from the array WHILE the loop is running. That won't work.
UnknownJoe796 is offline   Reply With Quote
Old 04-14-2010, 04:15 AM   #8
Parrot_King
Registered User
 
Join Date: Apr 2010
Posts: 6
Default

Quote:
Originally Posted by UnknownJoe796 View Post
Problem with your removal code is simple to fix. Replace your removal code for each enemy type with something like this:
ActionScript Code:
for each(var enemy in enemyArray) {    removeChild(enemy); } enemyArray=new Array();
For each statements are one of the niftiest things to come about in a long time - use them. Your code does not work because you are removing them from the array WHILE the loop is running. That won't work.
What do you mean by 'each' ?

so using my code

ActionScript Code:
for each(var f in farmerArray) {    removeChild(f); } farmerArray=new Array();

or do you mean var enemy needs to be defined earlier? Im using a class to spawn a movieclip, and the hit detection look to see if the array movieclip is on a certain keyframe.
im not real good at all this thats all lol
Parrot_King is offline   Reply With Quote
Old 04-14-2010, 05:12 AM   #9
rrh
throw a trace() in there
 
Join Date: Dec 2006
Posts: 1,982
Default

Okay, you're getting a little mistaken advice here.

Quote:
samuraiArray.removeChildAt(f)
1) removeChildAt removes children from Sprites and Movieclips, not from Arrays. You are correct to use splice to remove an entry from an array

Quote:
Originally Posted by UnknownJoe796 View Post
Your code does not work because you are removing them from the array WHILE the loop is running. That won't work.
2) No, it's okay because you're looping through the array from highest numbers to lowest.

Quote:
for each(var enemy in enemyArray)
{
removeChild(enemy);
}
enemyArray=new Array();
3) This would be another way to remove all the enemies, which is what your code is currently doing, but you said you want your code to only remove certain enemies, not all of them.

To that end, yes, you should have an if statement inside of your for loops to test if it's one that got hit. Sometimes you can use for this hitTestObject or hitTestPoint. You can also figure out the width and height of the two objects and compare them, like "if (player.x+20>enemy.x)&&player.x-20<enemy.x)&&..."

If you've already done the hitTest and the object that got hit is calling this function as a result of that you could have it pass itself as a parameter and use that to find itself in the array:
ActionScript Code:
function removeEnemy(enemy:Sprite):void { ... ...for loop ......if (enemy==array[f]) .........


As for your other question, yeah, a timer might be the way to go. You could set a variable for each, like farmerSpawned=true, and then start a timer that turns it back to false. Another way would be to hitTest the starting point to see if anything is already there.
rrh is offline   Reply With Quote
Old 04-14-2010, 06:54 AM   #10
Parrot_King
Registered User
 
Join Date: Apr 2010
Posts: 6
Default thank you...

ok i see what you are getting at and thanks for understanding what I wanted, im not very good at all this yet.

But when you state in the function "enemy" do you refer to the array or is this a new var?

Ill show you what I did and the error is inncorect number of statements exp 1


ActionScript Code:
//Kill the enemy and make them flash collecting the points function removeEnemy(enemy:Sprite):void { var f:int; //remove all enemys for (f=farmerArray.length-1; f>=0; f--) { if (enemy==farmerArray[f]) removeChild(farmerArray[f]); farmerArray.splice(f,1); } for (f=samuraiArray.length-1; f>=0; f--) { if (enemy==samuraiArray[f]) removeChild(samuraiArray[f]); samuraiArray.splice(f,1); } for (f=ninjaArray.length-1; f>=0; f--) { if (enemy==ninjaArray[f]) removeChild(ninjaArray[f]); ninjaArray.splice(f,1); } for (f=fatArray.length-1; f>=0; f--) { if (enemy==fatArray[f]) removeChild(fatArray[f]); fatArray.splice(f,1); } for (f=ninja2Array.length-1; f>=0; f--) {     if (enemy==ninja2Array[f])     removeChild(ninja2Array[f]); ninja2Array.splice(f,1); } trace("Remove") } function killEnemy():void {     enemyFlashTimer.reset();         enemyFlashTimer.start();     removeEnemy()     trace("KillEnemy"); }

The kill enemy function is called if the checkEnemyHit returns true. The enemy flash timer flashes the hit enemy but dose not work yet cause everything is removed from stage. Should i add the removeEnemy function out of the killEnemy and into the enemyFlashtimer so there not removed befor they flash???

But hey back to what im working on now... sorry if im a pain
Parrot_King 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 09:28 PM.

///
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.