Error #1009 sometimes during game - actionscript-3

I have made a game on Flash CS6 using AS3. The game has a spaceship on the right hand of the screen and it shoots bullets at aliens that randomly appear on the right. The game is all working perfectly but every now and then when I play it i get this error.
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at alien3/pulse()[/Users/Matt/Documents/DES B Assignment/Prototype/alien3.as:43]
Now here is the AS linkage for alien 3
package {
import flash.display.MovieClip;
import flash.events.Event;
public class alien3 extends MovieClip {
var yMove:Number = 15;
var changeDirectionAfterYMoves:Number = 0;
var moveCount:Number = 0;
var shootCount:Number = 0;
var shootMissilesAfterYMoves:Number = 0;
public function alien3() {
addEventListener(Event.ENTER_FRAME,pulse);
}
public function stopListening()
{
removeEventListener(Event.ENTER_FRAME,pulse);
}
public function pulse(evt:Event)
{
if (currentFrame!=1) return;
if(changeDirectionAfterYMoves == moveCount)
{
yMove=yMove*-1;
var maxMoves:Number;
if (yMove>0)maxMoves = Math.round (400-this.y)/yMove;
else maxMoves = Math.round((this.y)/Math.abs(yMove));
changeDirectionAfterYMoves = 1 + Math.round (Math.random () *maxMoves);
moveCount = 0;
}
if (shootCount==shootMissilesAfterYMoves)
{
var ao:alienMissile3 = new alienMissile3 ();
ao.x = this.x + 50;
ao.y = this.y;
parent.addChild(ao);
shootCount=0;
shootMissilesAfterYMoves = 1 + Math.round (Math.random() * 25)
}
this.y+=yMove;
moveCount = moveCount+1;
shootCount = shootCount+1;
}
}
}
So line 43 responds to the alien missile movie clip in the library
parent.addChild(ao);
Which is strange because it adds the alien missiles without a problem and they work fine
The alienMissile3 is a movie clip which was converted to a movie clip from a png which is named something different.
I have no idea what is causing this error.
The code in the main timeline is as follows.
import flash.display.MovieClip;
import flash.events.Event;
import flash.media.SoundChannel;
import flash.net.dns.AAAARecord;
var frameCount:Number = 0;
var alienCount:Number = 0;
var alienInterval:Number = 100;
var score:Number=0;
var gameOn:Boolean = false
var my_sound:laserGun = new laserGun();
var my_channel:SoundChannel = new SoundChannel();
var my_sound3:QueenAmidalaandTheNabooPalace = new QueenAmidalaandTheNabooPalace();
var my_channel3:SoundChannel = new SoundChannel();
var spaceshipMovement:Number = 0;
mcGameOverScreen.visible=false;
var scoresArray:Array = new Array();
var SO:SharedObject = SharedObject.getLocal("scores5");
if (SO.size!=0)
{
scoresArray = SO.data.scoresArray;
}
function startGame()
{
addEventListener(Event.ENTER_FRAME, pulse);
gameOn=true;
mySpaceship.livesLeft=3;
score=0;
}
function pulse(event:Event):void
{
if (mySpaceship.livesLeft<1) noLivesLeft();
addNewAlienIfNecessary();
/*addNewAlien2IfNecessary();
addNewAlien3IfNecessary();*/
checkForMissileOnAlien();
checkForMissileOnAlien2();
checkForMissileOnAlien3();
checkForMissileOnAlienMissile();
checkForMissileOnAlienMissile2();
checkForMissileOnAlienMissile3();
tidyUp();
checkForAlienMissileOnSpaceship();
checkForAlienMissile2OnSpaceship();
checkForAlienMissile3OnSpaceship();
tbScore.text = String(score);
tbLivesLeft.text = String (mySpaceship.livesLeft);
var bg:background = new background();
bg.y = 0;
bg.x = 0;
addChild(bg);
gameOn=true;
}
function addNewAlienIfNecessary()
{
if (score<=100 && alienCount<3 && frameCount%60==0)
{
var a:alien = new alien();
a.x = Math.random()*380;
a.y = Math.random()*50;
addChild(a);
alienCount=alienCount+1;
}
//frameCount++;
if (score >100 && score<500 && alienCount<3 && frameCount%80==0)
{
var aa:alien2 = new alien2();
aa.x = Math.random()*380;
aa.y = Math.random()*50;
addChild(aa);
alienCount=alienCount+1;
}
//frameCount++;
if (alienCount<3 && score>=500 && frameCount%100==0)
{
var ab:alien3 = new alien3();
ab.x = Math.random()*380;
ab.y = Math.random()*50;
addChild(ab);
alienCount=alienCount+1;
}
frameCount++;
}
/*function addNewAlien2IfNecessary()
{
if (score>100 && score<500 && alienCount<3 && frameCount%60==0)
{
var aa:alien2 = new alien2();
aa.x = Math.random()*440;
aa.y = Math.random()*50;
addChild(aa);
alienCount=alienCount+1;
}
frameCount++;
}
function addNewAlien3IfNecessary()
{
if (score>500 && alienCount<3 && frameCount%60==0)
{
var ab:alien3 = new alien3();
ab.x = Math.random()*440;
ab.y = Math.random()*50;
addChild(ab);
alienCount=alienCount+1;
}
frameCount++;
}*/
function checkForMissileOnAlien()
{
var missilez:Array = new Array;
var alienz:Array = new Array;
for (var i=0;i<numChildren;i++)
{
if (getChildAt(i) is alien) {alienz.push(getChildAt(i) as MovieClip)};
if (getChildAt(i) is missile) {missilez.push(getChildAt(i) as MovieClip)};
}
for (var j=0;j<alienz.length;j++)
{
for (var k=0;k<missilez.length;k++)
{
if (alienz[j].hitTestObject(missilez[k]))
{
alienz[j].gotoAndStop (2);
//alienz[j].stopListening();
missilez[k].gotoAndStop(2);
missilez[k].stopListening();
var e:explosion = new explosion();
e.x = alienz[j].x;
e.y = alienz[j].y;
addChild(e);
alienCount = alienCount-1;
score += 20;
}
}
}
}
function checkForMissileOnAlien2()
{
var missilez:Array = new Array;
var alienz2:Array = new Array;
for (var i=0;i<numChildren;i++)
{
if (getChildAt(i) is alien2) {alienz2.push(getChildAt(i) as MovieClip)};
if (getChildAt(i) is missile) {missilez.push(getChildAt(i) as MovieClip)};
}
for (var j=0;j<alienz2.length;j++)
{
for (var k=0;k<missilez.length;k++)
{
if (alienz2[j].hitTestObject(missilez[k]))
{
alienz2[j].gotoAndStop (2);
//alienz2[j].stopListening();
missilez[k].gotoAndStop(2);
missilez[k].stopListening();
var e:explosion = new explosion();
e.x = alienz2[j].x;
e.y = alienz2[j].y;
addChild(e);
alienCount = alienCount-1;
score += 50;
}
}
}
}
function checkForMissileOnAlien3()
{
var missilez:Array = new Array;
var alienz3:Array = new Array;
for (var i=0;i<numChildren;i++)
{
if (getChildAt(i) is alien3) {alienz3.push(getChildAt(i) as MovieClip)};
if (getChildAt(i) is missile) {missilez.push(getChildAt(i) as MovieClip)};
}
for (var j=0;j<alienz3.length;j++)
{
for (var k=0;k<missilez.length;k++)
{
if (alienz3[j].hitTestObject(missilez[k]))
{
alienz3[j].gotoAndStop (2);
//alienz3[j].stopListening();
missilez[k].gotoAndStop(2);
missilez[k].stopListening();
var e:explosion = new explosion();
e.x = alienz3[j].x;
e.y = alienz3[j].y;
addChild(e);
alienCount = alienCount-1;
score += 100;
}
}
}
}
function checkForMissileOnAlienMissile()
{
var alienMissilez:Array = new Array();
var missilez:Array = new Array();
for (var i=0;i<numChildren;i++)
{
if (getChildAt(i) is alienMissile) {alienMissilez.push(getChildAt(i) as MovieClip);}
if (getChildAt(i) is missile) {missilez.push(getChildAt(i) as MovieClip);}
}
trace("alienMissilez" + alienMissilez.length);
trace("missilez" + missilez.length);
for (var j=0;j<alienMissilez.length;j++)
{
for (var k=0;k<missilez.length;k++)
{
if (alienMissilez[j].hitTestObject(missilez[k]))
{
alienMissilez[j].gotoAndPlay(2);
alienMissilez[j].stopListening();
missilez[k].gotoAndPlay(2);
missilez[k].stopListening();
var e:explosion = new explosion();
e.x = alienMissilez[j].x;
e.y = alienMissilez[j].y;
addChild(e);
score += 10;
}
}
}
}
function checkForMissileOnAlienMissile2()
{
var alienMissilez2:Array = new Array();
var missilez:Array = new Array();
for (var i=0;i<numChildren;i++)
{
if (getChildAt(i) is alienMissile2) {alienMissilez2.push(getChildAt(i) as MovieClip);}
if (getChildAt(i) is missile) {missilez.push(getChildAt(i) as MovieClip);}
}
trace("alienMissilez2" + alienMissilez2.length);
trace("missilez" + missilez.length);
for (var j=0;j<alienMissilez2.length;j++)
{
for (var k=0;k<missilez.length;k++)
{
if (alienMissilez2[j].hitTestObject(missilez[k]))
{
alienMissilez2[j].gotoAndPlay(2);
alienMissilez2[j].stopListening();
missilez[k].gotoAndPlay(2);
missilez[k].stopListening();
var e:explosion = new explosion();
e.x = alienMissilez2[j].x;
e.y = alienMissilez2[j].y;
addChild(e);
score += 15;
}
}
}
}
function checkForMissileOnAlienMissile3()
{
var alienMissilez3:Array = new Array();
var missilez:Array = new Array();
for (var i=0;i<numChildren;i++)
{
if (getChildAt(i) is alienMissile3) {alienMissilez3.push(getChildAt(i) as MovieClip);}
if (getChildAt(i) is missile) {missilez.push(getChildAt(i) as MovieClip);}
}
trace("alienMissilez3" + alienMissilez3.length);
trace("missilez" + missilez.length);
for (var j=0;j<alienMissilez3.length;j++)
{
for (var k=0;k<missilez.length;k++)
{
if (alienMissilez3[j].hitTestObject(missilez[k]))
{
alienMissilez3[j].gotoAndPlay(2);
alienMissilez3[j].stopListening();
missilez[k].gotoAndPlay(2);
missilez[k].stopListening();
var e:explosion = new explosion();
e.x = alienMissilez3[j].x;
e.y = alienMissilez3[j].y;
addChild(e);
score += 20;
}
}
}
}
/*function tidyUp()
{
for (var j=numChildren-1;j>=0;j--)
{
if (getChildAt(j) is TextField) continue; //This added to help text box work
var mc:MovieClip = getChildAt(j) as MovieClip;
if (getChildAt(j) is explosion)
{
if (mc.currentFrame==15) removeChildAt(j);
continue;
}
//if (mc.x>800 || mc.x<0 || mc.currentFrame!=1 ) removeChildAt(j);
}
}
*/
function tidyUp()
{
for (var j=numChildren-1;j>=0;j--)
{
if (getChildAt(j) is TextField) continue;
if (getChildAt(j) is SimpleButton) continue;
var mc:MovieClip = getChildAt(j) as MovieClip;
if (getChildAt(j) is explosion)
{
if (mc.currentFrame==10) removeChildAt(j);
//continue;
}
if (getChildAt(j) is alienMissile)
{
if (mc.x>800||(mc.currentFrame==10)) removeChildAt(j);
//continue;
}
if (getChildAt(j) is alienMissile2)
{
if (mc.x>800||(mc.currentFrame==10)) removeChildAt(j);
//continue;
}
if (getChildAt(j) is alienMissile3)
{
if (mc.x>800||(mc.currentFrame==10)) removeChildAt(j);
//continue;
}
if(getChildAt(j) is missile)
{
if(mc.x<0||(mc.currentFrame==2)) removeChildAt(j);
//continue;
}
if(getChildAt(j) is alien)
{
if(mc.currentFrame!=1) removeChildAt(j);
//continue;
}
if(getChildAt(j) is alien2)
{
if(mc.currentFrame!=1) removeChildAt(j);
//continue;
}
if(getChildAt(j) is alien3)
{
if(mc.currentFrame!=1) removeChildAt(j);
//continue;
}
}
}
function checkForAlienMissileOnSpaceship()
{
for (var i=0;i<numChildren;i++)
{
if (getChildAt(i) is alienMissile)
{
if (getChildAt(i).hitTestObject(mySpaceship))
{
var mc:MovieClip = getChildAt(i) as MovieClip;
if (mc.currentFrame==1)
{
mc.gotoAndPlay(2);
mc.stopListening();
var e:explosion = new explosion();
e.x = mc.x;
e.y = mc.y;
addChild(e);
mySpaceship.isHit();
}
}
}
}
}
function checkForAlienMissile2OnSpaceship()
{
for (var i=0;i<numChildren;i++)
{
if (getChildAt(i) is alienMissile2)
{
if (getChildAt(i).hitTestObject(mySpaceship))
{
var mc:MovieClip = getChildAt(i) as MovieClip;
if (mc.currentFrame==1)
{
mc.gotoAndPlay(2);
mc.stopListening();
var e:explosion = new explosion();
e.x = mc.x;
e.y = mc.y;
addChild(e);
mySpaceship.isHit();
}
}
}
}
}
function checkForAlienMissile3OnSpaceship()
{
for (var i=0;i<numChildren;i++)
{
if (getChildAt(i) is alienMissile3)
{
if (getChildAt(i).hitTestObject(mySpaceship))
{
var mc:MovieClip = getChildAt(i) as MovieClip;
if (mc.currentFrame==1)
{
mc.gotoAndPlay(2);
mc.stopListening();
var e:explosion = new explosion();
e.x = mc.x;
e.y = mc.y;
addChild(e);
mySpaceship.isHit();
}
}
}
}
}
stage.addEventListener (KeyboardEvent.KEY_DOWN, spaceshipControls);
function spaceshipControls (event:KeyboardEvent):void
{
if (event.keyCode==38 && mySpaceship.y>+62 && stage.frameRate==24) {mySpaceship.y-=20;}
if (event.keyCode==40 && mySpaceship.y<480-mySpaceship.height && stage.frameRate==24) {mySpaceship.y+=20;}
if (event.keyCode==32 && gameOn==true && stage.frameRate==24)
{
var m:missile = new missile();
m.y = mySpaceship.y;
m.x = mySpaceship.x - 80;
addChild(m);
my_channel = my_sound.play();
}
}
btn_up.addEventListener(MouseEvent.MOUSE_DOWN, goUp);
btn_up.addEventListener(MouseEvent.MOUSE_UP, stopMoving);
function goUp(evt:MouseEvent)
{
if (mySpaceship.y>+62 && stage.frameRate==24)
{
mySpaceship.y-=20;
}
else
{
spaceshipMovement=0;
}
}
function stopMoving (evt:MouseEvent)
{
spaceshipMovement=0;
}
btn_down.addEventListener(MouseEvent.MOUSE_DOWN, goDown);
btn_down.addEventListener(MouseEvent.MOUSE_UP, stopMoving2);
function goDown(evt:MouseEvent)
{
if (mySpaceship.y<480-mySpaceship.height && stage.frameRate==24)
{
mySpaceship.y+=20;
}
else
{
spaceshipMovement=0;
}
}
function stopMoving2 (evt:MouseEvent)
{
spaceshipMovement=0;
}
btn_fire.addEventListener(MouseEvent.MOUSE_DOWN, fire);
function fire(evt:MouseEvent)
{
var m:missile = new missile();
m.y = mySpaceship.y;
m.x = mySpaceship.x - 80;
addChild(m);
my_channel = my_sound.play();
}
function noLivesLeft()
{
stage.removeEventListener(KeyboardEvent.KEY_DOWN, spaceshipControls);
btn_fire.removeEventListener(MouseEvent.MOUSE_DOWN, fire);
flash.media.SoundMixer.stopAll();
my_channel3 = my_sound3.play();
removeEventListener(Event.ENTER_FRAME,pulse);
for (var i=0;i<numChildren;i++)
{
//trace(i);
if (getChildAt(i) is TextField) continue;
if (getChildAt(i) is SimpleButton) continue;
var mc:MovieClip = getChildAt(i) as MovieClip;
if (mc.hasEventListener(Event.ENTER_FRAME)) mc.stopListening();
}
gameOn = false;
mcGameOverScreen.visible = true;
mcGameOverScreen.tb_score.text = String (score);
stage.removeEventListener(KeyboardEvent.KEY_DOWN, spaceshipControls);
btn_fire.removeEventListener(MouseEvent.MOUSE_DOWN, fire);
for (var c=numChildren-1;c>=0;c--)
{
var d = getChildAt(c);
if (d is alien || d is alienMissile|| d is explosion || d is missile || d is alien2 || d is alien3 ||d is alienMissile2 ||d is alienMissile3 || d is background) removeChildAt(c);
}
}
btnResume.addEventListener(MouseEvent.MOUSE_DOWN, resumeGame);
function resumeGame(e:MouseEvent):void
{
stage.frameRate = 24
btnPause.visible=true;
btnResume.visible=false;
}
btnPause.addEventListener(MouseEvent.MOUSE_DOWN, pauseGame);
function pauseGame(e:MouseEvent):void
{
stage.frameRate = 0
btnPause.visible=false;
btnResume.visible=true;
}
function setMute(vol)
{
var sTransform:SoundTransform = new SoundTransform(1,0);
sTransform.volume = vol;
SoundMixer.soundTransform = sTransform;
}
var isMuted:Boolean = false;
muteBtn.addEventListener(MouseEvent.CLICK,toggleMuteBtn);
function toggleMuteBtn(event:Event){
if(isMuted)
{
isMuted = false;
setMute(1);
}
else
{
isMuted = true;
setMute(0);
}
}

You are getting the error because you are checking for Alien3's parent before it has been added to the display list.
Add a trace before the parent.addChild(ao) line, ie:
trace("Alien3 parent = "+parent);
I'm betting the output will be null sometimes.
The solution is to replace the ENTER_FRAME event listener (in the Alien3 constructor function) with an ADDED_TO_STAGE event listener. Try this:
public function alien3() {
addEventListener(Event.ADDED_TO_STAGE, onStage);
}
private function onStage(e:Event):void {
removeEventListener(Event.ADDED_TO_STAGE, onStage);
addEventListener(Event.ENTER_FRAME, pulse);
}
Now, when pulse is called, 'parent' will not be null because the instance is on the display list and has access to 'parent'.

Related

how to fix error 1009 in flash as3

hi i have been trying to fix this by searching the net for hours and have had no luck so all help is appreciated (it happened when i put the snow in)
Thanks in Advance
Here's my code:
Quit.addEventListener(MouseEvent.CLICK, func22);
function func22(event:MouseEvent):void
{
gotoAndStop(2);
myTimer3.stop(); myTimer3.removeEventListener(TimerEvent.TIMER, timerListener3);
myTimer4.stop(); myTimer4.removeEventListener(TimerEvent.TIMER, timerListener4);
}
Help.addEventListener(MouseEvent.CLICK, func33);
function func33(event:MouseEvent):void
{
gotoAndStop(23);
myTimer3.stop(); myTimer3.removeEventListener(TimerEvent.TIMER, timerListener3);
myTimer4.stop(); myTimer4.removeEventListener(TimerEvent.TIMER, timerListener4);
}
var myTimer3:Timer = new Timer(2000,0);
myTimer3.addEventListener(TimerEvent.TIMER, timerListener3);
function timerListener3(e:TimerEvent):void {
Hungry_bar.scaleX-=0.05;
if(Hungry_bar.scaleX<=0.05){
gotoAndStop(12)
myTimer3.stop(); myTimer3.removeEventListener(TimerEvent.TIMER, timerListener);
myTimer4.stop(); myTimer4.removeEventListener(TimerEvent.TIMER, timerListener);
}
}
myTimer3.start();
var myTimer4:Timer = new Timer(3000,0);
myTimer4.addEventListener(TimerEvent.TIMER, timerListener4);
function timerListener4(e:TimerEvent):void {
Fun_bar.scaleX-=0.05;
if(Fun_bar.scaleX<=0.05){
gotoAndStop(13)
myTimer3.stop(); myTimer3.removeEventListener(TimerEvent.TIMER, timerListener3);
myTimer4.stop(); myTimer4.removeEventListener(TimerEvent.TIMER, timerListener4);
}
}
myTimer4.start();
Feed.addEventListener(MouseEvent.CLICK,feed2)
function feed2(e:MouseEvent){
Hungry_bar.scaleX+=0.05
if(Hungry_bar.scaleX>=1.5){
gotoAndStop(14)
myTimer3.stop(); myTimer3.removeEventListener(TimerEvent.TIMER, timerListener3);
myTimer4.stop(); myTimer4.removeEventListener(TimerEvent.TIMER, timerListener4);
}
}
Fun.addEventListener(MouseEvent.CLICK,happy2)
function happy2(e:MouseEvent){
Fun_bar.scaleX+=0.05
if(Fun_bar.scaleX>=1.5){
gotoAndStop(15)
myTimer3.stop(); myTimer3.removeEventListener(TimerEvent.TIMER, timerListener);
myTimer4.stop(); myTimer4.removeEventListener(TimerEvent.TIMER, timerListener);
}
}
//snow
var snowflakes:Array = new Array();
var snowflakeProps:Dictionary= new Dictionary(true);
var max_snowsize:Number = .04;
// pixels
var snowflakesCnt:Number = 150;
var oheight:Number;
var owidth:Number;
init();
function init():void {
owidth = width;
oheight = height;
// quantity
for (var i:int=0; i<snowflakesCnt; i++) {
var t:MovieClip = new SnowFlake();//
t.name = "snowflake"+i;
t.alpha = .2+Math.random()*.6;
t.x = -(owidth/2)+Math.random()*(1.5*owidth);
t.y = -(oheight/2)+Math.random()*(1.5*oheight);
t.scaleX = t.scaleY=.5+Math.random()*(max_snowsize*10);
var o:Object = new Object();
o.k = 1+Math.random()*2;
o.wind = -1.5+Math.random()*(1.4*3);
snowflakeProps[t] = o;
addChild(t);
snowflakes.push(t);
}
addEventListener(Event.ENTER_FRAME, snowFlakeMover);
}
function shakeUp():void{
for (var i:int=0; i<snowflakes.length; i++) {
var t:MovieClip = snowflakes[i] as MovieClip;
t.x = -(owidth/2)+Math.random()*(1.5*owidth);
t.y = -(oheight/2)+Math.random()*(1.5*oheight);
}
}
function snowFlakeMover(evt:Event):void {
var dO:MovieClip;
var o :Object;
if(visible && parent.visible){
for (var i:int = 0; i < snowflakes.length; i++) {
dO = snowflakes[i] as MovieClip;
o = snowflakeProps[dO];
dO.y += o.k;
dO.x += o.wind;
if (dO.y>oheight+10) {
dO.y = -20;
}
if (dO.x>owidth+20) {
dO.x = -(owidth/2)+Math.random()*(1.5*owidth);
dO.y = -20;
} else if (dO.x<-20) {
dO.x= -(owidth/2)+Math.random()*(1.5*owidth);
dO.y = -20;
}
}
}
}
and here is the error:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at petgamewithweather_fla::MainTimeline/frame2()[petgamewithweather_fla.MainTimeline::frame2:9]
at flash.display::MovieClip/gotoAndStop()
at petgamewithweather_fla::MainTimeline/func22()[petgamewithweather_fla.MainTimeline::frame16:5]

How do I remove eventlisteners and objects from an array upon game completion?

I have written a drag and drop game and, for the most part, it works.
It runs and does what I need it to do.
However, I cannot figure out two things.
How to remove the toy objects from the screen and re-add them after game over.
How to remove the event listeners for dragging/collision action after game over has initiated.
At the moment, after score is displayed you can still drop items in the toybox and make the score rise even when game over has been displayed.
Does anyone have any idea what I am doing wrong?
I would love to figure this out but it is driving me crazy.
Any help would be great.
Here is my code ...
package {
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.text.Font;
import flash.filters.GlowFilter;
public class MainGame extends MovieClip {
const BG_SPEED:int = 5;
const BG_MIN:int = -550;
const BG_MAX:int = 0;
const PBG_SPEED:int = 3;
var bg:BackGround = new BackGround;
var paraBg:ParaBg = new ParaBg;
var toybox:TargetBox = new TargetBox;
var toy:Toy = new Toy;
var tryAgain:TryAgain = new TryAgain;
var cheer:Cheer = new Cheer;
var eightBit:EightBit = new EightBit;
var countDown:Number = 30;
var myTimer:Timer = new Timer(1000, 30);
var myText:TextField = new TextField;
var myText2:TextField = new TextField;
var myTextFormat:TextFormat = new TextFormat;
var myTextFormat2:TextFormat = new TextFormat;
var font1:Font1 = new Font1;
var kids:Kids = new Kids;
var count:int = 0;
var finalScore:int = 0;
var score:Number = 0;
var toy1:Toy1 = new Toy1;
var toy2:Toy2 = new Toy2;
var toy3:Toy3 = new Toy3;
var toy4:Toy4 = new Toy4;
var toy5:Toy5 = new Toy5;
var toy6:Toy6 = new Toy6;
var toy7:Toy7 = new Toy7;
var toy8:Toy8 = new Toy8;
var toy9:Toy9 = new Toy9;
var toy10:Toy10 = new Toy10;
var toy11:Toy11 = new Toy11;
var toy12:Toy12 = new Toy12;
var toy13:Toy13 = new Toy13;
var toy14:Toy14 = new Toy14;
var toy15:Toy15 = new Toy15;
var toy16:Toy16 = new Toy16;
var toy17:Toy17 = new Toy17;
var toy18:Toy18 = new Toy18;
var toy19:Toy19 = new Toy19;
var toy20:Toy20 = new Toy20;
var toyArray:Array = new Array(toy1, toy2, toy3, toy4, toy5, toy6, toy7, toy8, toy9, toy10, toy11, toy12, toy13, toy14, toy15, toy16, toy17, toy18, toy19, toy20);
public function mainGame():void
{
trace("HI");
eightBit.play(0, 9999);
addChildAt(paraBg, 0);
addChildAt(bg, 1);
addChildAt(kids, 2);
kids.x = 310;
kids.y = 200;
addChild(toy);
toy.x = 306;
toy.y = 133;
addChild(toybox);
toybox.x = 295;
toybox.y = 90;
function addToys(xpos:int, ypos:int)
{
addChild(toyArray[i]);
toyArray[i].x = xpos;
toyArray[i].y = ypos;
}
for (var i:int = 0; i < toyArray.length; i++)
{
addToys(1140 * Math.random() + 20, 170 * Math.random() + 230);
}
}
public function bgScroll (e:Event)
{
stage.addEventListener(MouseEvent.MOUSE_UP, arrayDrop);
myTimer.addEventListener(TimerEvent.TIMER, countdown);
myTimer.addEventListener(TimerEvent.TIMER_COMPLETE, timerDone);
myTimer.start();
e.target.addEventListener(Event.ENTER_FRAME, collision);
if (stage.mouseX > 600 && bg.x > BG_MIN)
{
bg.x -= BG_SPEED;
paraBg.x -= PBG_SPEED;
for (var m:int=0; m< toyArray.length; m++)
{
(toyArray[m] as MovieClip).x -=BG_SPEED
}
}
else if (stage.mouseX < 50 && bg.x < BG_MAX)
{
bg.x += BG_SPEED;
paraBg.x += PBG_SPEED;
for (var j:int=0; j< toyArray.length; j++)
{
(toyArray[j] as MovieClip).x +=BG_SPEED
}
}
for (var k:int = 0; k < toyArray.length; k++)
{
toyArray[k].addEventListener(MouseEvent.MOUSE_DOWN, arrayGrab);
}
bg.addEventListener(Event.ENTER_FRAME, bgScroll);
} // End of BGScroll
public function collision (e:Event)
{
for (var l:int=0; l< toyArray.length; l++)
{
if (toyArray[l].hitTestObject(toy))
{
removeChild(toyArray[l]);
toyArray[l].x=100000;
toybox.gotoAndPlay(2);
cheer.play(1, 1);
score = score + 10;
trace(score);
}
if (score == 200)
{
timerDone();
myTimer.stop();
}
}
}
public function arrayGrab(e:MouseEvent)
{
e.target.startDrag();
}
public function arrayDrop(e:MouseEvent)
{
stopDrag();
}
public function resetGame(e:Event):void {
trace("CLICK");
countDown = 30;
myText.text = "0" + countDown.toString();
myTimer.addEventListener(TimerEvent.TIMER_COMPLETE, timerDone);
myTimer.reset();
removeChild(tryAgain);
myText2.visible = false;
score = 0;
}
public function countdown(e:TimerEvent):void
{
if (countDown > 0)
{
countDown--;
}
if (countDown < 10)
{
myText.text = "0" + countDown.toString();
myText.x = 270;
displayText();
}
else if (countDown < 20 && countDown > 9)
{
myText.text = countDown.toString();
myText.x = 280;
displayText();
}
else
{
myText.text = countDown.toString();
myText.x = 270;
displayText();
}
} // end of countdown function
public function displayText():void
{
myText.filters = [new GlowFilter(0x00FF00, 1.0, 5, 5, 4)];
addChild(myText);
myText.width = 500, myText.height = 50, myText.y = 10;
myTextFormat.size = 50, myTextFormat.font = font1.fontName;
myText.setTextFormat(myTextFormat);
}
public function displayText2():void
{ myText2.filters = [new GlowFilter(0xFF0000, 1.0, 5, 5, 4)];
addChild(myText2);
myText2.width = 500, myText2.height = 35, myText2.x = 204, myText2.y = 200;
myTextFormat2.size = 30, myTextFormat2.font = font1.fontName;
myText2.setTextFormat(myTextFormat2);
}
public function timerDone(e:TimerEvent=null):void
{
if (countDown == 0)
{
count = 0;
finalScore = score;
}
else
{
count = (30) - (myTimer.currentCount);
finalScore = (count * 10) + (score);
}
myText.text = "GAME OVER!";
myText.x = 195;
displayText();
myText2.text = "Your score = " + (finalScore);
displayText2();
addChild(tryAgain);
tryAgain.x = 300;
tryAgain.y = 300;
tryAgain.addEventListener(MouseEvent.CLICK, resetGame);
}
} // End of class
} //End of package
Nevermind ... solved it.
Easiest was was to change
function addToys(xpos:int, ypos:int)
{
addChild(toyArray[i]);
toyArray[i].x = xpos;
toyArray[i].y = ypos;
}
To
function addToys(xpos:int, ypos:int)
{
stage.addChild(toyArray[i]);
toyArray[i].x = xpos;
toyArray[i].y = ypos;
}
And then, in the timerDone function I added thisif statement ...
for (var m = 0; m < toyArray.length; m++) {
if (stage.contains(toyArray[m])) {
stage.removeChild(toyArray[m]);
}
Worked a treat!

Error 1180, Flash

So, the same college project as last time, and this time im having a trouble with undefined methods. The entirity of my code is below
package
{
import flash.display.MovieClip;
import flash.events.MouseEvent;
public class Lesson_05 extends MovieClip
{
private static const boardWidth:uint = 4;
private static const boardHeight:uint = 2;
private static const cardHorizontalSpacing:Number = 52;
private static const cardVerticalSpacing:Number = 52;
private static const boardOffsetX:Number = 171;
private static const boardOffsetY:Number = 148;
private var firstCard:Card;
private var secondCard:Card;
private var cardsLeft:uint;
var startPage:StartPage_1;
var matchPage:MatchPage_1;
var guessPage:GuessPage_1;
var startMessage:String;
var mysteryNumber:uint;
var currentGuess:uint;
var guessesRemaining:uint;
var guessesMade:uint;
var gameStatus:String;
var gameWon:Boolean;
public function Lesson_05():void
{
startPage = new StartPage_1();
matchPage = new MatchPage_1();
guessPage = new GuessPage_1();
addChild(startPage);
startPage.matchButton.addEventListener(MouseEvent.CLICK,onMatchButtonClick);
startPage.guessButton_1.addEventListener(MouseEvent.CLICK,onGuessButtonClick_1);
guessPage.matchButton.addEventListener(MouseEvent.CLICK,onMatchButtonClick_Guess);
//Output Errors #2025 when added this line;
guessPage.startButton.addEventListener(MouseEvent.CLICK,onStartButtonClick);
matchPage.startButton.addEventListener(MouseEvent.CLICK,onStartButtonClick_Match);
matchPage.guessButton_1.addEventListener(MouseEvent.CLICK,onGuessButtonClick_Match_1);
}
function onMatchButtonClick(event:MouseEvent):void
{
addChild(matchPage);
removeChild(startPage);
match();
}
function onGuessButtonClick_1(event:MouseEvent):void
{
addChild(guessPage);
removeChild(startPage);
guess();
}
function onMatchButtonClick_Guess(event:MouseEvent):void
{
addChild(matchPage);
removeChild(guessPage);
match();
}
function onStartButtonClick(event:MouseEvent):void
{
addChild(startPage);
removeChild(guessPage);
}
function onStartButtonClick_Match(event:MouseEvent):void
{
addChild(startPage);
removeChild(matchPage);
}
function onGuessButtonClick_Match_1(event:MouseEvent):void
{
addChild(guessPage);
removeChild(matchPage);
guess();
}
function guess():void
{
startMessage = "I am thinking of a number between 1 and 20";
mysteryNumber = Math.ceil(Math.random()*20);
guessesRemaining = 10;
guessesMade = 0;
gameStatus = "";
gameWon = false;
guessPage.output_txt.text = startMessage;
guessPage.input_txt.text = "";
guessPage.input_txt.backgroundColor = 0xFFCCCCCC;
guessPage.input_txt.restrict = "0-9";
guessPage.stage.focus = guessPage.input_txt;
guessPage.guessButton_2.enabled = true;
guessPage.guessButton_2.alpha = 1;
guessPage.againButton_1.visible = false;
guessPage.guessButton_2.addEventListener(MouseEvent.CLICK,onGuessButtonClick_2);
}
function onGuessButtonClick_2(event:MouseEvent):void
{
guessesRemaining--;
guessesMade++;
gameStatus = "Guesses Remaining: " + guessesRemaining + ", GuessesMade:" + guessesMade;
currentGuess = uint(guessPage.input_txt.text);
if (currentGuess > mysteryNumber)
{
guessPage.output_txt.text = "That's too high!" + "\n" + gameStatus;
checkGameOver();
}
else if (currentGuess < mysteryNumber)
{
guessPage.output_txt.text = "That's too low!" + "\n" + gameStatus;
checkGameOver();
}
else
{
//guessPage.output_txt.text = "Well Done! You got it!";
gameWon = true;
endGame();
}
function checkGameOver():void
{
if (guessesRemaining < 1)
{
endGame();
}
}
function endGame():void
{
if (gameWon)
{
guessPage.output_txt.text = "Yes, it's " + mysteryNumber + "!" + "\n" + "It only took you " + guessesMade + " guesses!";
}
else
{
guessPage.output_txt.text = "Sorry, you've run out of guesses!" + "\n" + "The correct number was " + mysteryNumber + ".";
}
guessPage.guessButton_2.removeEventListener(MouseEvent.CLICK,onGuessButtonClick_2);
guessPage.guessButton_2.enabled = false;
guessPage.guessButton_2.alpha = 0.5;
guessPage.againButton_1.visible = true;
guessPage.againButton_1.addEventListener(MouseEvent.CLICK,onAgainButtonClick_1);
}
function onAgainButtonClick_1(event:MouseEvent):void
{
guess();
guessPage.againButton_1.removeEventListener(MouseEvent.CLICK,onAgainButtonClick_1);
}
function match():void
{
var cardlist:Array = new Array();
for (var i:uint=0; i<boardWidth*boardHeight/2; i++)
{
cardlist.push(i);
cardlist.push(i);
}
cardsLeft = 0;
for (var x:uint=0; x<boardWidth; x++)
{
for (var y:uint=0; y<boardHeight; y++)
{
var c:Card = new Card();
c.stop();
c.x = x * cardHorizontalSpacing + boardOffsetX;
c.y = y * cardVerticalSpacing + boardOffsetY;
var r:uint = Math.floor(Math.random() * cardlist.length);
c.cardface = cardlist[r];
cardlist.splice(r,1);
c.addEventListener(MouseEvent.CLICK,clickCard);
addChild(c);
cardsLeft++;
}
}
}
function clickCard(event:MouseEvent)
{
var thisCard:Card = (event.target as Card);
if (firstCard ==null)
{
firstCard = thisCard;
firstCard.gotoAndStop(thisCard.cardface+2);
}
else if (firstCard ==thisCard)
{
firstCard.gotoAndstop(1);
firstCard = null;
}
else if (secondCard == null)
{
secondCard = thisCard;
secondCard.gotoAndStop(thisCard.cardface+2);
if (firstCard.cardface == secondCard.cardface)
{
removeChild(firstCard);
removeChild(secondCard);
firstCard = null;
secondCard = null;
cardsLeft -= 2;
if (cardsLeft ==0)
{
}
}
}
else
{
firstCard.gotoAndStop(1);
secondCard.gotoAndStop(1);
secondCard = null;
firstCard = thisCard;
firstCard.gotoAndStop(thisCard.cardface+2);
}
}
}
}
}
and the problems I'm getting are:
1180: Call to a possibly undefined method match.
I have very little knowledge with AS3, and have spent more than 2 hours trying to resolve this problem. Thanks in advance guys, PS any links to tutorials etc are massively appreciated
You misplaced close braces'}' of the function onGuessButtonClick_2. Use the below code and check it.
package
{
import flash.display.MovieClip;
import flash.events.MouseEvent;
public class Lesson_05 extends MovieClip
{
private static const boardWidth:uint = 4;
private static const boardHeight:uint = 2;
private static const cardHorizontalSpacing:Number = 52;
private static const cardVerticalSpacing:Number = 52;
private static const boardOffsetX:Number = 171;
private static const boardOffsetY:Number = 148;
private var firstCard:Card;
private var secondCard:Card;
private var cardsLeft:uint;
var startPage:StartPage_1;
var matchPage:MatchPage_1;
var guessPage:GuessPage_1;
var startMessage:String;
var mysteryNumber:uint;
var currentGuess:uint;
var guessesRemaining:uint;
var guessesMade:uint;
var gameStatus:String;
var gameWon:Boolean;
public function Lesson_05():void
{
startPage = new StartPage_1();
matchPage = new MatchPage_1();
guessPage = new GuessPage_1();
addChild(startPage);
startPage.matchButton.addEventListener(MouseEvent.CLICK,onMatchButtonClick);
startPage.guessButton_1.addEventListener(MouseEvent.CLICK,onGuessButtonClick_1);
guessPage.matchButton.addEventListener(MouseEvent.CLICK,onMatchButtonClick_Guess);
//Output Errors #2025 when added this line;
guessPage.startButton.addEventListener(MouseEvent.CLICK,onStartButtonClick);
matchPage.startButton.addEventListener(MouseEvent.CLICK,onStartButtonClick_Match);
matchPage.guessButton_1.addEventListener(MouseEvent.CLICK,onGuessButtonClick_Match_1);
}
function onMatchButtonClick(event:MouseEvent):void
{
addChild(matchPage);
removeChild(startPage);
match();
}
function onGuessButtonClick_1(event:MouseEvent):void
{
addChild(guessPage);
removeChild(startPage);
guess();
}
function onMatchButtonClick_Guess(event:MouseEvent):void
{
addChild(matchPage);
removeChild(guessPage);
match();
}
function onStartButtonClick(event:MouseEvent):void
{
addChild(startPage);
removeChild(guessPage);
}
function onStartButtonClick_Match(event:MouseEvent):void
{
addChild(startPage);
removeChild(matchPage);
}
function onGuessButtonClick_Match_1(event:MouseEvent):void
{
addChild(guessPage);
removeChild(matchPage);
guess();
}
function guess():void
{
startMessage = "I am thinking of a number between 1 and 20";
mysteryNumber = Math.ceil(Math.random()*20);
guessesRemaining = 10;
guessesMade = 0;
gameStatus = "";
gameWon = false;
guessPage.output_txt.text = startMessage;
guessPage.input_txt.text = "";
guessPage.input_txt.backgroundColor = 0xFFCCCCCC;
guessPage.input_txt.restrict = "0-9";
guessPage.stage.focus = guessPage.input_txt;
guessPage.guessButton_2.enabled = true;
guessPage.guessButton_2.alpha = 1;
guessPage.againButton_1.visible = false;
guessPage.guessButton_2.addEventListener(MouseEvent.CLICK,onGuessButtonClick_2);
}
function onGuessButtonClick_2(event:MouseEvent):void
{
guessesRemaining--;
guessesMade++;
gameStatus = "Guesses Remaining: " + guessesRemaining + ", GuessesMade:" + guessesMade;
currentGuess = uint(guessPage.input_txt.text);
if (currentGuess > mysteryNumber)
{
guessPage.output_txt.text = "That's too high!" + "\n" + gameStatus;
checkGameOver();
}
else if (currentGuess < mysteryNumber)
{
guessPage.output_txt.text = "That's too low!" + "\n" + gameStatus;
checkGameOver();
}
else
{
//guessPage.output_txt.text = "Well Done! You got it!";
gameWon = true;
endGame();
}
}
function checkGameOver():void
{
if (guessesRemaining < 1)
{
endGame();
}
}
function endGame():void
{
if (gameWon)
{
guessPage.output_txt.text = "Yes, it's " + mysteryNumber + "!" + "\n" + "It only took you " + guessesMade + " guesses!";
}
else
{
guessPage.output_txt.text = "Sorry, you've run out of guesses!" + "\n" + "The correct number was " + mysteryNumber + ".";
}
guessPage.guessButton_2.removeEventListener(MouseEvent.CLICK,onGuessButtonClick_2);
guessPage.guessButton_2.enabled = false;
guessPage.guessButton_2.alpha = 0.5;
guessPage.againButton_1.visible = true;
guessPage.againButton_1.addEventListener(MouseEvent.CLICK,onAgainButtonClick_1);
}
function onAgainButtonClick_1(event:MouseEvent):void
{
guess();
guessPage.againButton_1.removeEventListener(MouseEvent.CLICK,onAgainButtonClick_1);
}
function match():void
{
var cardlist:Array = new Array();
for (var i:uint=0; i<boardWidth*boardHeight/2; i++)
{
cardlist.push(i);
cardlist.push(i);
}
cardsLeft = 0;
for (var x:uint=0; x<boardWidth; x++)
{
for (var y:uint=0; y<boardHeight; y++)
{
var c:Card = new Card();
c.stop();
c.x = x * cardHorizontalSpacing + boardOffsetX;
c.y = y * cardVerticalSpacing + boardOffsetY;
var r:uint = Math.floor(Math.random() * cardlist.length);
c.cardface = cardlist[r];
cardlist.splice(r,1);
c.addEventListener(MouseEvent.CLICK,clickCard);
addChild(c);
cardsLeft++;
}
}
}
function clickCard(event:MouseEvent)
{
var thisCard:Card = (event.target as Card);
if (firstCard ==null)
{
firstCard = thisCard;
firstCard.gotoAndStop(thisCard.cardface+2);
}
else if (firstCard ==thisCard)
{
firstCard.gotoAndstop(1);
firstCard = null;
}
else if (secondCard == null)
{
secondCard = thisCard;
secondCard.gotoAndStop(thisCard.cardface+2);
if (firstCard.cardface == secondCard.cardface)
{
removeChild(firstCard);
removeChild(secondCard);
firstCard = null;
secondCard = null;
cardsLeft -= 2;
if (cardsLeft ==0)
{
}
}
}
else
{
firstCard.gotoAndStop(1);
secondCard.gotoAndStop(1);
secondCard = null;
firstCard = thisCard;
firstCard.gotoAndStop(thisCard.cardface+2);
}
}
}
}
Hope it helps.
Your match() function is inside the onGuessButtonClick_2(MouseEvent) function.
So when you try it from anywhere else that is not the inside of the onGuessButtonClick_2 function, flash gets confused because it cannot find the function match() because it is only callable inside the onGuessButtonClick_2() function.
1180 errors are caused by Flash being dead.
1180 Error: Flash is Dead, please convert your code to HTML5 or do it natively.
okthxbai
Adobe

How can I use a function parameter to refer to a variable?

Sorry if this question is a bit vague, but this has been driving me nuts recently. It's nothing too complicated, but all I want to do is have the variable 'targetVariable' be affected by a formula. The actual problem lies in the fact that the referenced variable, being 'masterVolume' in this case, is not getting affected by the formula, but rather 'targetVariable' instead. I run the 'makeSlider' function at the bottom of the script. Here's the code:
var masterVolume:Number = 0;
var panning:Number = 0;
function makeSlider(sliderType, X, Y, targetVariable) {
var sliderHandle:MovieClip = new sliderType();
addChild(sliderHandle);
sliderHandle.x = X;
sliderHandle.y = Y;
var dragging:Boolean = false;
stage.addEventListener(Event.ENTER_FRAME, updateSlider);
function updateSlider(e:Event):void {
panning = (mouseX/(stage.stageWidth/2))-1;
targetVariable = ((sliderHandle.x-bar.x)/bar.width);
output.text = masterVolume.toString();
if (dragging == true && mouseX >= bar.x && mouseX <= (bar.x + bar.width)) {
sliderHandle.x = mouseX;
}
}
sliderHandle.addEventListener(MouseEvent.MOUSE_DOWN, beginDrag);
function beginDrag(e:MouseEvent):void {
dragging = true;
}
stage.addEventListener(MouseEvent.MOUSE_UP, endDrag);
function endDrag(e:MouseEvent):void {
dragging = false;
}
}
function playSound(target, intensity:Number, pan:Number) {
var sound:Sound = new target();
var transformer:SoundTransform = new SoundTransform(intensity, pan);
var channel:SoundChannel = new SoundChannel();
channel=sound.play();
channel.soundTransform = transformer;
}
stage.addEventListener(MouseEvent.MOUSE_DOWN, make);
function make(e:MouseEvent):void {
playSound(test, masterVolume, panning);
}
makeSlider(SliderHandle, bar.x, bar.y, masterVolume);
Okay, so I studied the Object class and found out that I could reference the variable by making it an object in the function. Here's the updated, working script:
var panning:Number = 0;
var masterVolume:Number = 0;
function makeSlider(sliderType, barType, soundType, hitBoxScale:Number, X, Y, targetVariable) {
var reference:Object = { targetVariable: 0 };
var slider:MovieClip = new sliderType;
var newBar:MovieClip = new barType;
addChild(newBar);
newBar.x = X;
newBar.y = Y;
addChild(slider);
slider.x = X;
slider.y = Y;
var dragging:Boolean = false;
stage.addEventListener(Event.ENTER_FRAME, updateSlider);
function updateSlider(e:Event):void {
panning = (mouseX/(stage.stageWidth/2))-1;
reference.targetVariable = (slider.x-newBar.x)/newBar.width;
if (dragging == true && mouseX >= newBar.x && mouseX <= (newBar.x + newBar.width)) {
slider.x = mouseX;
}
if (reference.targetVariable <= 0.01) {
output.text = ("None");
}
if (reference.targetVariable >= 0.99) {
output.text = ("Max");
}
if (reference.targetVariable > 0.01 && reference.targetVariable < 0.99) {
output.text = (Math.round((reference.targetVariable*100))+"%").toString();
}
}
stage.addEventListener(MouseEvent.MOUSE_DOWN, beginDrag);
function beginDrag(e:MouseEvent):void {
if (mouseY >= newBar.y-hitBoxScale && mouseY <= (newBar.y + newBar.height)+hitBoxScale) {
dragging = true;
}
}
slider.addEventListener(MouseEvent.MOUSE_DOWN, beginDragFromSlider);
function beginDragFromSlider(e:MouseEvent):void {
dragging = true;
}
stage.addEventListener(MouseEvent.MOUSE_UP, endDrag);
function endDrag(e:MouseEvent):void {
if (dragging == true) {
playSound(soundType, reference.targetVariable, 0);
}
dragging = false;
}
stage.addEventListener(MouseEvent.MOUSE_DOWN, make);
function make(e:MouseEvent):void {
if (dragging == false) {
playSound(test, reference.targetVariable, panning);
}
}
}
function playSound(target, intensity:Number, pan:Number) {
var sound:Sound = new target();
var transformer:SoundTransform = new SoundTransform(intensity, pan);
var channel:SoundChannel = new SoundChannel();
channel=sound.play();
channel.soundTransform = transformer;
}
makeSlider(defaultSlider, defaultBar, volumeIndicator, 10, 134, 211, masterVolume);

as3 child as movieclip - won't accept functions

this follows on from my last question which I thought was answered but for some reason when I treat a child of my stage (display object) as a movieclip I can't then apply the usual functions that I want to:
var mc1:MovieClip = this.getChildByName("mc1") as MovieClip;
if(mc1) {
mc1.useHandCursor = true;
mc1.buttonMode = true;
mc1.addEventListener(MouseEvent.CLICK, fillDriveway);
}
Any wisdom would greatly appreciated... and sorry for asking such a similar question as previous...
Thanks in advance.
EDIT: More code from the AS on this project for context:
import flash.display.*
ImageUploader.visible = false;
function showUploader(e:MouseEvent):void {
ImageUploader.visible = true;
ImageUploader.gotoAndPlay(2);
}
pic.addEventListener(MouseEvent.CLICK,addNewPoint);
var n:Number = 0;
var joinPointsArray:Array = new Array;
function addNewPoint(e:MouseEvent):void {
n++;
pointNo.text = String(n);
if(n==1){
var nextPoint:MovieClip = new mcstart();
addChild(nextPoint);
nextPoint.name = "mc"+pointNo.text;
nextPoint.x = e.target.mouseX;
nextPoint.y = e.target.mouseY;
}else{
var nextPoint2:MovieClip = new newPoint();
addChild(nextPoint2);
nextPoint2.name = "mc"+pointNo.text;
nextPoint2.x = e.target.mouseX;
nextPoint2.y = e.target.mouseY;
}
var joinPoints:MovieClip = new MovieClip();
this.addChild(joinPoints);
joinPointsArray.push(joinPoints);
joinPoints.graphics.lineStyle(0.5,0xFF0000);
joinPoints.graphics.moveTo(this.getChildByName("mc1").x, this.getChildByName("mc1").y);
for(var i:int=2; i<=n; ++i){
joinPoints.graphics.lineTo(this.getChildByName("mc"+i).x, this.getChildByName("mc"+i).y);
}
}
pic.addEventListener(MouseEvent.CLICK, addNewPoint);
function fillDriveway(eventObject:MouseEvent) {
var joinPoints:MovieClip = new MovieClip();
this.addChild(joinPoints);
for(var p:int=0; p<(joinPointsArray.length); ++p) {
joinPointsArray[p].alpha = 0;
}
this.getChildByName("mc1").alpha = 0;
joinPoints.graphics.beginFill(0xFFFFFF, 0.7);
joinPoints.graphics.moveTo(this.getChildByName("mc1").x, this.getChildByName("mc1").y);
for(var m:int=2; m<=n; ++m){
joinPoints.graphics.lineTo(this.getChildByName("mc"+m).x, this.getChildByName("mc"+m).y);
}
joinPoints.name = "driveshape";
filledDrive.text = "filled";
}
function undoit(eventObject:MouseEvent) {
if(n > 0) {
if(filledDrive.text.indexOf("filled") != -1) {
this.removeChild(this.getChildAt(this.numChildren -1));
filledDrive.text = "";
}else{
this.removeChild(this.getChildAt(this.numChildren -1));
this.removeChild(this.getChildAt(this.numChildren -1));
n--;
pointNo.text = String(n);
}
}
}
function maskDrive(eventObject:MouseEvent) {
if(filledDrive.text.indexOf("filled") != -1) {
var finishA:MovieClip = new finishMC();
this.addChild(finishA);
finishA.x = 310;
finishA.y = 100;
finishA.mask = getChildByName("driveshape");
finishA.gotoAndPlay(2);
}
}
//BTN RollOvers
function btn1over(myEvent:MouseEvent) {
btn1.gotoAndPlay(2);
}
function btn1out(myEvent:MouseEvent) {
btn1.gotoAndPlay(11);
}
function btn2over(myEvent:MouseEvent) {
btn2.gotoAndPlay(2);
}
function btn2out(myEvent:MouseEvent) {
btn2.gotoAndPlay(11);
}
function btn3over(myEvent:MouseEvent) {
btn3.gotoAndPlay(2);
}
function btn3out(myEvent:MouseEvent) {
btn3.gotoAndPlay(11);
}
function undoover(myEvent:MouseEvent) {
undo.gotoAndPlay(2);
}
function undoout(myEvent:MouseEvent) {
undo.gotoAndPlay(11);
}
//BTN Calls
btn1HIT.addEventListener(MouseEvent.CLICK, fillDriveway);
btn1HIT.addEventListener(MouseEvent.ROLL_OVER, btn1over);
btn1HIT.addEventListener(MouseEvent.ROLL_OUT, btn1out);
btn1HIT.buttonMode = true;
btn1HIT.useHandCursor = true;
btn2HIT.addEventListener(MouseEvent.CLICK, maskDrive);
btn2HIT.addEventListener(MouseEvent.ROLL_OVER, btn2over);
btn2HIT.addEventListener(MouseEvent.ROLL_OUT, btn2out);
btn2HIT.buttonMode = true;
btn2HIT.useHandCursor = true;
btn3HIT.buttonMode = true;
btn3HIT.useHandCursor = true;
btn3HIT.addEventListener(MouseEvent.ROLL_OVER, btn3over);
btn3HIT.addEventListener(MouseEvent.ROLL_OUT, btn3out);
btn3HIT.addEventListener(MouseEvent.CLICK, showUploader);
undoHIT.addEventListener(MouseEvent.CLICK, undoit);
undoHIT.addEventListener(MouseEvent.ROLL_OVER, undoover);
undoHIT.addEventListener(MouseEvent.ROLL_OUT, undoout);
undoHIT.buttonMode = true;
undoHIT.useHandCursor = true;
var mc1:MovieClip = this.getChildByName("mc1") as MovieClip;
if(mc1) {
mc1.useHandCursor = true;
mc1.buttonMode = true;
mc1.addEventListener(MouseEvent.CLICK, fillDriveway);
}
Are you sure the movieclip is placed on stage or actually converted to movieclip?
Try stage.getChildByName(). Where did you place this code? Inside a frame or inside a main document class? To be sure you can check the childs are added on stage and see what their names are.
You can use this code
for ( var i :int = 0; i < this.numChildren; i++ )
{
babe = this.getChildAt( i );
if ( babe is MovieClip) {
trace( babe.name);
}
}
I've also seen this, not sure if it works.
if (stage.contains(mc1)) {
}
Wahoo! Figured it out!
By popping
var mc1:MovieClip = this.getChildByName("mc1") as MovieClip;
at the end of my AS I was referring to the child "mc1" before it existed - it isn't created until the user clicks somewhere on the "pic" movieclip. So the solution was to bung my actions for "mc1" (including declaring it as a MovieClip) into a separate function:
function createstartEndMC():void {
var startEnd:MovieClip = (this.getChildByName("mc1")) as MovieClip;
startEnd.useHandCursor = true;
startEnd.buttonMode = true;
startEnd.addEventListener(MouseEvent.CLICK, fillDriveway);
}
and then to call this function AFTER "mc1" child is created:
function addNewPoint(e:MouseEvent):void {
n++;
pointNo.text = String(n);
if(n==1){
var nextPoint:MovieClip = new mcstart();
addChild(nextPoint);
nextPoint.name = "mc"+pointNo.text;
nextPoint.x = e.target.mouseX;
nextPoint.y = e.target.mouseY;
createstartEndMC();
}
Finally it works and "mc1" (or "startEnd" as I call it once it's created and made into an MC) finally behaves as a normal timeline MC should!
I'm so happy - thanks for all your guidance!
Cam