Cannot move the frame - actionscript-3

I made a catcher game, and got into trouble when the time runs out can not move the frame
What I already have done:
function addMc()
{
var NewMc = new MC();
NewMc.y = Math.ceil(Math.random() * 500);
NewMc.x = 50;
addChildAt(NewMc,1);
pelotas.push(NewMc);
NewMc.addEventListener(Event.ENTER_FRAME, dropMc);
}
function dropMc(e:Event)
{
var b:MC = MC(e.target);
b.x += 10;
if (b.x > 900)
{
eliminasiMC(b);
}
}
stage.addEventListener(Event.ENTER_FRAME, catcher);
function catcher(e:Event)
{
catcherMC.y = mouseY;
for (var i:int=0; i<pelotas.length; i++)
{
if (catcher.hitTestObject(pelotas[i]))
{
eliminasiMC(pelotas[i]);
point++;
txtPoint.text = String(point);
}
}
}
function eliminasiMC(B)
{
B.removeEventListener(Event.ENTER_FRAME, dropMc);
removeChild(B);
}
var tTime:Number = 5;
var timer:Timer = new Timer(1000,tTime);
timer.addEventListener(TimerEvent.TIMER, countdown);
timer.start();
function countdown(event:TimerEvent)
{
txtTime.text=String((tTime)-timer.currentCount);
if (txtTime.text == "0")
{
stage.removeEventListener(Event.ENTER_FRAME, catcher);
timer.stop();
timer.removeEventListener(TimerEvent.TIMER, countdown);
gotoAndStop("nextFrame"); // HERE MY PROBLEM
}
}
But this error occurs:
TypeError: Error #1009: Cannot access a property or method of a null object reference. at GEBUGENDE_fla::MainTimeline/addBabi()[GEBUGENDE_fla.MainTimeline::frame80:18] at Function/http://adobe.com/AS3/2006/builtin::apply() at SetIntervalTimer/onTimer() at flash.utils::Timer/_timerDispatch() at flash.utils::Timer/tick()

Related

Shoot in different directions in as3

I'm having some troubles with the game I'm making. My character shoots bullets, but when it comes to shooting them in different directions it does not work properly. When it shoots the bullets follow the character's direction. Below I post the code.
var Bulli:Array = new Array();
var ShootTimer:Timer = new Timer(0);
stage.addEventListener(MouseEvent.MOUSE_DOWN, startShootTimer);
stage.addEventListener(MouseEvent.MOUSE_UP, stopShootTimer);
ShootTimer.addEventListener(TimerEvent.TIMER, shootBullo);
stage.addEventListener(Event.ENTER_FRAME, mainLoop);
function startShootTimer(e:MouseEvent):void
{
ShootTimer.start();
}
function stopShootTimer(e:MouseEvent):void
{
ShootTimer.stop();
}
function shootBullo(e:TimerEvent):void
{
var bullo:Bullo = new Bullo();
bullo.x = Jumbo.x;
bullo.y = Jumbo.y - 50;
if(destra)
{
bullo.dir = destra;
}
else
{
bullo.dir = sinistra;
}
addChild(bullo);
Bulli.push(bullo);
}
function mainLoop (e:Event):void
{
for (var b:int = 0; b < Bulli.length; b++)
{
if (Bulli[b].dir == destra)
{
Bulli[b].x += 10;
}
else
{
Bulli[b].x -= 10;
}
}
}
Don't add that listener to Stage, instead add it to each unique bullo...
//# not to Stage...
//stage.addEventListener(Event.ENTER_FRAME, mainLoop);
Try this (untested but may be useful for some ideas):
function shootBullo(e:TimerEvent):void
{
var bullo:Bullo = new Bullo();
bullo.x = Jumbo.x;
bullo.y = Jumbo.y - 50;
if(destra) { bullo.dir = destra; }
else { bullo.dir = sinistra; }
bullo.addEventListener(Event.ENTER_FRAME, mainLoop);
}
function mainLoop (e:Event):void //the "e" of this function parameter is each unique "Bullo"
{
//# currentTarget is whichever "bullo" is talking to this Event (via Listener).
if (e.currentTarget.dir == destra)
{ e.currentTarget.x += 10; }
else { e.currentTarget.x -= 10; }
}

Flash - RemoveChild Actionscript 3 dont work delete

I made a game that is based on collecting stars. I have a problem that is at the moment of meeting 4 stars want to share passed to the next frame and removed all the frames tried removeChild() but a message pops up :
TypeError: Error #1009: Cannot access a property or method of a null object reference.
health_txt.text=health.toString()
score_txt.text=score.toString();
var health=20;
var score=0;
var intervalPunkty = setInterval(addGwiazda,1000);
function addGwiazda()
{
var gwiazda:Gwiazda = new Gwiazda();
gwiazda.x = Math.ceil(Math.random() * 550);
gwiazda.y = -50;
addChild(gwiazda);
gwiazda.addEventListener(Event.ENTER_FRAME, dropGwiazda);
function dropGwiazda(e:Event)
{
var b:Gwiazda = Gwiazda(e.target);
b.y += 10;
if(b.y > 400)
{
b.removeEventListener(Event.ENTER_FRAME, dropGwiazda);
removeChild(b);
}
if(jazda.hitTestObject(b))
{
score ++;
score_txt.text = score.toString();
b.removeEventListener(Event.ENTER_FRAME, dropGwiazda);
removeChild(b);
if (score == 4){
gotoAndStop(15);
removeChild(b);
}
}
}
}
stop();
score_txt.text=score.toString();
var health=20;
var score=0;
var intervalPunkty = setInterval(addGwiazda,1000);
function addGwiazda()
{
var gwiazda:Gwiazda = new Gwiazda();
gwiazda.x = Math.ceil(Math.random() * 550);
gwiazda.y = -50;
addChild(gwiazda);
gwiazda.addEventListener(Event.ENTER_FRAME, dropGwiazda);
function dropGwiazda(e:Event)
{
var b:Gwiazda = Gwiazda(e.target);
b.y += 10;
if(b.y > 400)
{
b.removeEventListener(Event.ENTER_FRAME, dropGwiazda);
if(contains(b))
{
removeChild(b);
}
}
if(jazda.hitTestObject(b))
{
score ++;
score_txt.text = score.toString();
b.removeEventListener(Event.ENTER_FRAME, dropGwiazda);
if(contains(b))
{
removeChild(b);
}
if (score == 4){
gotoAndStop(15);
}
}
}
}
stop();`enter code here`

Error #1006: removeChild is not a function

So I'm trying to create a game where in there's an object falling from the middle and you have to drag it in the left if it's good or right if it's bad.
What I'm having problems with right now is I don't know how the program would know if the object is good or bad. I think.
I'm getting an error:
Error #1006: removeChild is not a function.
I'm newbie at flash, if you have tips or whatever, please share!
http://pastebin.com/AnpN6tEy
import flash.events.Event;
var tray:Array = new Array(Legal2_1,Legal2_2,Legal2_3,Legal2_4,Legal2_5,Legal2_6,Legal2_7,Legal2_8,Legal2_9,Legal2_10,Legal2_11,Legal2_12,Legal2_13,Legal2_14,Legal2_15,Illegal2_1,Illegal2_2,Illegal2_3,Illegal2_4,Illegal2_5,Illegal2_6,Illegal2_7,Illegal2_8,Illegal2_9,Illegal2_10,Illegal2_11,Illegal2_12,Illegal2_13,Illegal2_14,Illegal2_15);
var traypos:int;
var goodpos:int;
var badpos:int;
traypos = (stage.stageWidth / 2)-100;
goodpos = ((stage.stageWidth / 3) -100);
badpos = (((stage.stageWidth/3) *2) -100);
var timerT:Timer = new Timer(1000,120);
timerT.addEventListener(TimerEvent.TIMER, traytimerhandler);
timerT.start();
var secondsT:Number = 1;
function traytimerhandler(event:TimerEvent)
{
//trace("Seconds elapsed: " + seconds);
SpawnTray(null);
secondsT++;
}
function SpawnTray(event:Event):void
{
var trayspawn:int;
trayspawn = int(Math.random() * tray.length);
var trayn:MovieClip = new tray[trayspawn]();
addChild(trayn);
trayn.x = traypos;
trayn.y = -20;
trayn.addEventListener(Event.ENTER_FRAME, MoveTray(trayspawn));
trayn.addEventListener(MouseEvent.MOUSE_DOWN, startDragging);
//trayn.addEventListener(MouseEvent.CLICK, CheckTray(trayspawn));
}
function MoveTray(trayc:int):Function
{
return function(event:Event):void {
var trayn:DisplayObject = event.target as DisplayObject;
trayn.y += 5;
if (trayn.y >= stage.stageHeight + 50)
{
CheckTray(trayc);
trayn.removeEventListener(Event.ENTER_FRAME, MoveTray);
this.removeChild(trayn);
}
}
}
function startDragging(e:MouseEvent):void
{
e.target.removeEventListener(MouseEvent.MOUSE_DOWN, startDragging);
e.target.removeEventListener(Event.ENTER_FRAME, MoveTray);
// surprise! This object will not be moved via MOUSE_DOWN,;
// because it's already being moved
// e.target.addEventListener(
e.target.addEventListener(MouseEvent.MOUSE_UP, stopDragging);
e.target.startDrag();
}
// drag;
function stopDragging(e:MouseEvent):void
{
e.target.stopDrag();
e.target.addEventListener( Event.ENTER_FRAME, MoveTray);
e.target.addEventListener(MouseEvent.MOUSE_DOWN, startDragging);
e.target.removeEventListener(MouseEvent.MOUSE_UP,stopDragging);
return;// emergency exit. We don't need to do more
}
function CheckTray(trayspawn:int):Function
{
return function(event:Event):void {
var trayn:DisplayObject = event.target as DisplayObject;
if (trayn.x <= goodpos)
{
//good side
if (trayspawn<=14)
{
score += 15;
}
else
{
score -= 15;
}
}
if (trayn.x >= badpos)
{
//bad side
if (trayspawn<=14)
{
score -= 15;
}
else
{
score += 15;
}
}
if (trayn.x > goodpos && trayn.x < badpos)
{
//middle
score -= 15;
}
}
}
Implicit coercion error on line 37 is caused by the fact that addEventListener expects function. Change the line to:
trayn.addEventListener(Event.ENTER_FRAME, MoveTray);
"Undefined property event" problems have common cause. Event argument is missing in the signatures of the functions. They should be like this:
function MoveTray(event:Event):void
function CheckTray(event:Event):void
you can access the object that is dispatching the Event with the property target of the Event.
ex:
var m1:MovieClip = new MovieClip();
m1.name = 'm1';
m1.addEventListener( Event.ENTER_FRAME, onEnterFrame );
var m2:MovieClip = new MovieClip();
m2.name = 'm2';
m2.addEventListener( Event.ENTER_FRAME, onEnterFrame );
function onEnterFrame(e:Event):void
{
trace( 'onEnterFrame', e.target.name ); // you could see m1 and m2
}
Hope this could help you

ActionScript 3: Error #2025

My code works fine. It's suppose to bring in one image when the user presses 1 and swap it out for another when he / she presses 2. But, when I presses 1 or 2 after having previously pressing either the same number I get a #2025 error. Ex: Pressing 1 then pressing 1 again.
ArgumentError: Error #2025: The supplied DisplayObject must be a child
of the caller.
at flash.display::DisplayObjectContainer/removeChild()
at warren_fla::MainTimeline/reportKeyDown2()
Code
import flash.events.KeyboardEvent;
var bdata = new image1(stage.stageWidth, stage.stageHeight);
var bdata2 = new image2(stage.stageWidth, stage.stageHeight);
var bmp = new Bitmap(bdata);
var bmp2 = new Bitmap(bdata2);
function reportKeyDown(event:KeyboardEvent):void
{
if (event.keyCode == 49) {
//trace("1 is pressed");
bmp.x = 230;
bmp.y = 150;
addChild(bmp);
}
if (contains(bmp2)) {
removeChild(bmp2);
}
}
function reportKeyDown2(event:KeyboardEvent):void
{
if (event.keyCode == 50) {
//trace("2 is pressed");
bmp2.x = 230;
bmp2.y = 150;
addChild(bmp2);
removeChild(bmp);
}
}
stage.addEventListener(KeyboardEvent.KEY_DOWN, reportKeyDown);
stage.addEventListener(KeyboardEvent.KEY_DOWN, reportKeyDown2);
You are removing bmp without checking if it is already a child.
function reportKeyDown2(event:KeyboardEvent):void
{
if (event.keyCode == 50) {
//trace("2 is pressed");
bmp2.x = 230;
bmp2.y = 150;
addChild(bmp2);
if(contains(bmp))
removeChild(bmp);
}
}
Also your code can be refactored into this simpler version :
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
var bdata = new image1(stage.stageWidth, stage.stageHeight);
var bdata2 = new image2(stage.stageWidth, stage.stageHeight);
var bmp = new Bitmap(bdata);
var bmp2 = new Bitmap(bdata2);
function reportKeyDown(event:KeyboardEvent):void
{
if (event.keyCode == Keyboard.NUMBER_1) {
swapBitmaps(bmp, bmp2);
} else if (event.keyCode == Keyboard.NUMBER_2) {
swapBitmaps(bmp2, bmp);
}
}
function swapBitmaps(first:Bitmap, second:Bitmap):void
{
first.x = 230;
first.y = 150;
addChild(first);
if(contains(second)) {
removeChild(second);
}
}
stage.addEventListener(KeyboardEvent.KEY_DOWN, reportKeyDown);
In reportKeyDown(), try moving:
if (contains(bmp2)) {
removeChild(bmp2);
}
Inside the if statement that checks the keycode:
function reportKeyDown(event:KeyboardEvent):void
{
if (event.keyCode == 49) {
//trace("1 is pressed");
bmp.x = 230;
bmp.y = 150;
addChild(bmp);
if (contains(bmp2)) {
removeChild(bmp2);
}
}
}
And in reportKeyDown2, check to make sure that bmp is a child before removing it:
function reportKeyDown2(event:KeyboardEvent):void
{
if (event.keyCode == 50) {
//trace("2 is pressed");
bmp2.x = 230;
bmp2.y = 150;
addChild(bmp2);
if(contains(bmp))
{
removeChild(bmp);
}
}
}
You're potentially adding the bmp's multiple times. Same with removing them, but if you try to remove a child that isn't on the display list, you'll get the error you're seeing. Try using this code in all cases where you're adding or removing:
if (!contains(bmp)) { addChild(bmp); } // or bmp2
if (contains(bmp)) { removeChild(bmp); } // or bmp2
----------- edit -------------
OK, while obviously you can addChild without checking (since addChild will remove the object from the display tree), it's more efficient to do the check. In fact, it seems to be about 3x faster. This test:
var clip = new MovieClip();
var i,j,tin,dur;
var tries = 100000;
for (j=0; j<5; j++) {
trace("-------------------");
tin = new Date().time;
for (i=0; i<tries; i++) { if (!contains(clip)) { addChild(clip); } }
dur = new Date().time - tin;
trace("Check Adding: "+dur);
removeChild(clip);
tin = new Date().time;
for (i=0; i<tries; i++) { addChild(clip); }
dur = new Date().time - tin;
trace("Just Adding: "+dur);
}
outputs:
-------------------
Check Adding: 9
Just Adding: 25
-------------------
Check Adding: 8
Just Adding: 25
-------------------
Check Adding: 9
Just Adding: 24
-------------------
Check Adding: 9
Just Adding: 24
-------------------
Check Adding: 9
Just Adding: 25

1046 AS3 Compile Time Error

I'm having major issues with this one error
//Obligitory Stop
stop();
//Imports
import flash.events.Event;
import flash.events.KeyboardEvent;
import fl.motion.easing.Back;
import flash.events.MouseEvent;
import flash.accessibility.Accessibility;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.display.MovieClip;
//Variables
var bulletSpeed:uint = 20;
var scoreData:int;
var bullets:Array = new Array();
var killCounter:int;
var baddieCounter:int;
var currentLevel:int = 1;
var baddieDamage:int;
var energyCost:int;
var target:MovieClip;
var baddies:Array = new Array();
var timer:Timer = new Timer(1);
var baddieSpeed:int;
var score:int;
var levelKR:int;
var level1KR:int = 10;
var level2KR:int = 25;
var level3KR:int = 50;
var upPressed:Boolean = false;
var downPressed:Boolean = false;
var Baddie:MovieClip
var mySound:Sound = new ShootSFX();
//Level Atributes set
if (currentLevel == 1)
{
baddieSpeed = 2;
baddieDamage = 20;
timer.delay = 4000;
levelKR = level1KR;
bulletSpeed = 10;
energyCost = 50;
var energyTimer:Timer = new Timer(50);
var healthTimer:Timer = new Timer(1000);
}
//Event Listeners
stage.addEventListener(MouseEvent.MOUSE_DOWN, fireGun);
stage.addEventListener(Event.ENTER_FRAME, moveObjects);
timer.addEventListener(TimerEvent.TIMER, addBaddie);
rbDash.addEventListener(Event.ENTER_FRAME, fl_MoveInDirectionOfKey);
stage.addEventListener(KeyboardEvent.KEY_DOWN, fl_SetKeyPressed);
stage.addEventListener(KeyboardEvent.KEY_UP, fl_UnsetKeyPressed);
//Timers Start
timer.start();
energyTimer.start();
healthTimer.start();
//Initialize score
Score.text = String("Level "+currentLevel+" - begin!");
//load score data
score = scoreData;
//Checks Kill Counter
checkKillCounter();
//Shoot gun on space
function fireGun(evt:KeyboardEvent)
{
if (evt.keyCode == Keyboard.SPACE)
{
bullet.x = rbDash.x;
bullet.y = rbDash.y + 50;
addChild(bullet);
bullets.push(bullet);
}
}
//Move Objects
function moveObjects(evt:Event):void
{
moveBullets();
moveBaddies();
}
//Move bullets
function moveBullets():void
{
for (var i:int = 0; i < bullets.length; i++)
{
var dx = Math.cos(deg2rad(bullets[i].rotation)) * bulletSpeed;
var dy = Math.sin(deg2rad(bullets[i].rotation)) * bulletSpeed;
bullets[i].x += dx;
bullets[i].y += dy;
if (bullets[i].x <-bullets[i].width
|| bullets[i].x > stage.stageWidth + bullets[i].width
|| bullets[i].y < -bullets[i].width
|| bullets[i].y > stage.stageHeight + bullets[i].width)
{
removeChild(bullets[i]);
bullets.splice(i, 1);
}
}
}
//Spawns Enemy
function addBaddie(evt:TimerEvent):void
{
updateScore(25);
var baddie:Baddie = new Baddie();
var side:Number = Math.ceil(Math.random() * 4);
if (side == 1)
{
baddie.x = Math.random() * stage.stageWidth;
baddie.y = - baddie.height;
}
else if (side == 2)
{
baddie.x = stage.stageWidth + baddie.width;
baddie.y = Math.random() * stage.stageHeight;
}
else if (side == 3)
{
baddie.x = Math.random() * stage.stageWidth;
baddie.y = stage.stageHeight + baddie.height;
}
else if (side == 4)
{
baddie.x = - baddie.width;
baddie.y = Math.random() * stage.stageHeight;
}
baddie.speed = baddieSpeed;
addChild(baddie);
baddies.push(baddie);
baddieCounter += 1;
if (baddieCounter == levelKR)
{
timer.stop();
}
}
//Moves Enemy
function moveBaddies():void
{
for (var i:int = 0; i < baddies.length; i++)
{
var dx = Math.cos(deg2rad(baddies[i].angle)) * baddies[i].speed;
var dy = Math.sin(deg2rad(baddies[i].angle)) * baddies[i].speed;
baddies[i].x += dx;
baddies[i].y += dy;
if (baddies[i].hitTestPoint(rbDash.x,rbDash.y,true))
{
removeChild(baddies[i]);
baddies.splice(i, 1);
//HealthBar.gotoAndStop(HealthBar.currentFrame + baddieDamage);
killCounter += 1;
checkKillCounter();
//if (HealthBar.currentFrame == 100)
{
gotoAndStop(5);
}
}
else
{
checkForHit(baddies[i]);
}
}
}
//Hit detection
function checkForHit(baddie:Baddie):void {//Level 1, Layer 'Actions', Frame 1, Line 166 1046: Type was not found or was not a compile-time constant: Baddie.
for (var i:int = 0; i < bullets.length; i++)
{
if (baddie.hitTestPoint(bullets[i].x,bullets[i].y,true))
{
removeChild(baddie);
removeChild(bullets[i]);
baddies.splice(baddie.indexOf(baddie), 1);
bullets.splice(bullets[i]);
updateScore(100);
killCounter += 1;
checkKillCounter();
}
}
}
//Updates score
function updateScore(points:int):void
{
score += points;
Score.text = String("Points: "+score);
}
//stops timers
function timerStop():void
{
timer.stop();
energyTimer.stop();
healthTimer.stop();
}
//Y axis movement
//totaly not a code snippet
function fl_MoveInDirectionOfKey(event:Event)
{
if (upPressed)
{
rbDash.y -= 5;
}
if (downPressed)
{
rbDash.y += 5;
}
}
function fl_SetKeyPressed(event:KeyboardEvent):void
{
switch (event.keyCode)
{
case Keyboard.UP :
{
upPressed = true;
break;
};
case Keyboard.DOWN :
{
downPressed = true;
break;
}
}
};
function fl_UnsetKeyPressed(event:KeyboardEvent):void
{
switch (event.keyCode)
{
case Keyboard.UP :
{
upPressed = false;
break;
};
case Keyboard.DOWN :
{
downPressed = false;
break;
}
}
};
//makes the deg2rad work for the bullets/enemy
function deg2rad(degree)
{
return degree * (Math.PI / 180);//Had issues with "deg2rad" functions
}
//Removes listeners
function removeAllListeners():void
{
stage.removeEventListener(MouseEvent.MOUSE_DOWN, fireGun);
stage.removeEventListener(Event.ENTER_FRAME, moveObjects);
timer.removeEventListener(TimerEvent.TIMER, addBaddie);
}
//Checks if level end
function checkKillCounter():void
{
EnemiesLeft.text = ("Enemies Left: "+String(levelKR - killCounter));
if (killCounter == levelKR)
{
shutdown();
gotoAndStop(3);
}
}
//Stops everything
function shutdown():void
{
timerStop();
removeAllListeners();
removeChild(target);
}
I get Level 1, Layer 'Actions', Frame 1, Line 166 1046: Type was not found or was not a compile-time constant: Baddie.
Thanks Guys
Im trying to get this done today so i can move on
Is Baddie a class that is in the default package? If not, you need to import it:
import packagename.Baddie;
If Baddie is a library symbol, make sure you've checked 'Export for ActionScript' and that the linkage name is correct. Also make sure that is is exported in frame 1, or at least before or on the frame that your code is on.
In you Library, right click on Baddie and choose "properties" and check "export for ActionScript". Now you can use Baddie as a class that extends MovieClip.
Sorry, I didn't notice this was already recommended. Basically, your error cannot find a Class named Baddie, hence why you need to specify the custom class through the Library instance.