PDA

View Full Version : [AS3] Game and Watch Help


Parrot_King
04-11-2010, 12:30 AM
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!!!

UnknownJoe796
04-12-2010, 01:58 AM
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.

rrh
04-13-2010, 03:20 PM
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.

Parrot_King
04-14-2010, 02:36 AM
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.

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

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!!!!

Artofwork
04-14-2010, 02:44 AM
try

samuraiArray.removeChildAt(f)

Parrot_King
04-14-2010, 03:00 AM
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()

UnknownJoe796
04-14-2010, 03:34 AM
Problem with your removal code is simple to fix. Replace your removal code for each enemy type with something like this:
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.

Parrot_King
04-14-2010, 04:15 AM
Problem with your removal code is simple to fix. Replace your removal code for each enemy type with something like this:
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

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

rrh
04-14-2010, 05:12 AM
Okay, you're getting a little mistaken advice here.

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

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.

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

Parrot_King
04-14-2010, 06:54 AM
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


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

UnknownJoe796
04-14-2010, 08:46 PM
@rrh Ah, I did not realize that. Stupid on my part. I should have read the both his code and his post more carefully.
@ParrotKing - Ignore the code I posted - it won't help. However, I do highly recommend merging the enemy arrays into one single array to simplify your code.