as3 order directory by date/time - actionscript-3

I need you help to fin a solution to order an array by creation date time.
I try this but this function seems to order only by date and not by time together.
private function triImgDate(folder:Array):void {
var acDirectory:ArrayCollection = new ArrayCollection();
for each (var tmp:File in folder) {
if (!tmp.isDirectory) {
if (tmp.extension != null && ArrayFunction.inArray(tabExt, tmp.extension.toLowerCase()))
{
listeImages.addItem(tmp);
}
}
else {
triImgDate(tmp.getDirectoryListing());
}
}
var dataSortField:SortField = new SortField();
dataSortField.name = "creationDate";
dataSortField.numeric = false;
var numericDataSort:Sort = new Sort();
numericDataSort.fields = [dataSortField];
listeImages.sort = numericDataSort;
listeImages.refresh();
}
So if you can help me.
Best regards
// solution
private function triImgDate(folder:Array):void {
var acDirectory:ArrayCollection = new ArrayCollection();
var arTempListeImages:ArrayCollection = new ArrayCollection();
var arTempListeCompar:ArrayCollection = new ArrayCollection();
var objImg:Object;
for each (var tmp:File in folder) {
if (!tmp.isDirectory) {
if (tmp.extension != null && ArrayFunction.inArray(tabExt, tmp.extension.toLowerCase())) {
objImg= new Object();
//listeImages.addItem(tmp);
arTempListeImages.addItem(tmp);
objImg.creationDate=tmp.creationDate;
objImg.url=tmp.url;
objImg.pos=arTempListeCompar.length;
arTempListeCompar.addItem(objImg)
}
} else {
triImgDate(tmp.getDirectoryListing());
}
}
var dataSortField:SortField = new SortField();
dataSortField.name = "creationDate";
dataSortField.compareFunction = function (a:Object, b:Object) : int {
var na:Number = a.creationDate.getTime();
var nb:Number = b.creationDate.getTime();
if (na < nb)
return -1;
if (na > nb)
return 1;
return 0;
};
var numericDataSort:Sort = new Sort();
numericDataSort.fields = [dataSortField];
arTempListeCompar.sort= numericDataSort;
arTempListeCompar.refresh();
var cpt:int=0;
for each (var objTem:Object in arTempListeCompar) {
listeImages.addItem(arTempListeImages[arTempListeCompar[cpt].pos]);
cpt++;
}
arTempListeImages=[];
arTempListeCompar=[];
acDirectory=[];
}

What you're trying to do here, is comparing twe dates by their string representation, which is obviously not a good way to do it.
you should rather use compareFunction property of the SortField object. Something like that (not tested):
private function triImgDate(folder:Array):void {
var acDirectory:ArrayCollection = new ArrayCollection();
for each (var tmp:File in folder) {
if (!tmp.isDirectory) {
if (tmp.extension != null && ArrayFunction.inArray(tabExt, tmp.extension.toLowerCase())) {
listeImages.addItem(tmp);
}
} else {
triImgDate(tmp.getDirectoryListing());
}
}
var dataSortField:SortField = new SortField();
dataSortField.name = "creationDate";
dataSortField.sortFunction = function(date1:Date, date2:Date):int {
const t1:uint = date1.time;
const t2:uint = date2.time;
if (t1 < t2) {
return -1;
}
if (t1 == t2) {
return 0;
}
return 1;
}
var numericDataSort:Sort = new Sort();
numericDataSort.fields = [dataSortField];
listeImages.sort = numericDataSort;
listeImages.refresh();
}

Related

Create an XML file from a script AS3

I can not figure out how I get more XML file from the language images,
In the image here you will look at setupItems how it is built
Here it explains how I build the missing XML file but I do not understand how I build it properly that will work for me on the site
public function init(param1:int) : void
{
var _loc2_:String = null;
this.itemHolder.addChild(this.hairHolder);
this.itemHolder.addChild(this.shirtHolder);
this.shirtHolder.visible = false;
this.itemHolder.addChild(this.pantsHolder);
this.pantsHolder.visible = false;
this.itemHolder.addChild(this.shoesHolder);
this.shoesHolder.visible = false;
this.activeHolder = this.hairHolder;
this.initButtons();
this.randomTimer = new Timer(this.randomTimerInitDelay,this.randomTimerCount);
this.randomTimer.addEventListener(TimerEvent.TIMER,this.changeRandomClothes,false,0,true);
this.xml = new XML();
if(param1 == 1)
{
_loc2_ = "boyItems.xml";
}
else if(param1 == 2)
{
_loc2_ = "girlItems.xml";
}
this.xmlLoader = new URLLoader(new URLRequest("./website/common/register/xmls/" + _loc2_));
this.xmlLoader.addEventListener(Event.COMPLETE,this.xmlLoaded);
this.xmlLoader.addEventListener(IOErrorEvent.IO_ERROR,this.errorLoadingXml);
}
another code
private function xmlLoaded(param1:Event) : void
{
this.xmlLoader.removeEventListener(Event.COMPLETE,this.xmlLoaded);
this.xmlLoader.removeEventListener(IOErrorEvent.IO_ERROR,this.errorLoadingXml);
this.xml = XML(this.xmlLoader.data);
Register.AVATAR_HAIR = this.xml.hair.child(0).#id;
var _loc2_:int = Math.random() * 2 + 1;
this.bubble.gotoAndStop("hair_" + Register.language + _loc2_);
this.setupItems(this.xml.hair,this.hairItems,this.hairHolder);
this.setupItems(this.xml.shirt,this.shirtItems,this.shirtHolder);
this.setupItems(this.xml.pants,this.pantsItems,this.pantsHolder);
this.setupItems(this.xml.shoes,this.shoesItems,this.shoesHolder);}
private function setupItems(param1:*, param2:Array, param3:Sprite) : void
{
var _loc9_:* = undefined;
var _loc10_:Item = null;
var _loc4_:int = 54.5;
var _loc5_:Number = 64.5;
var _loc6_:*;
var _loc7_:int = (_loc6_ = param1).children().length();
var _loc8_:int = 0;
while(_loc8_ < _loc7_)
{
_loc9_ = _loc6_.child(_loc8_);
(_loc10_ = new Item()).x = _loc4_;
_loc4_ += this.itemSpacing;
_loc10_.y = _loc5_;
_loc10_.loadItem(_loc9_.#type,_loc9_.#ordinal,_loc9_.#id,_loc9_.#inventoryType,Register.pathToItemsFolder);
param2.push(_loc10_);
param3.addChild(_loc10_);
_loc8_++;
}
}
This is the loadItem file:
(Maybe here you can see how I build the XML files)
public function loadItem(param1:int, param2:int, param3:int, param4:int, param5:String) : void
{
this.type = param1;
this.ordinal = param2;
this.id = param3;
this.itemImage = new ImageItemNoCache(param1,param2,param3,param4,null,null,param5);
this.itemImage.mouseEnabled = false;
this.itemImage.mouseChildren = false;
this.itemImage.addEventListener(ImageItem.DATA_LOADED,this.positionImage,false,0,true);
addChild(this.itemImage);
addEventListener(MouseEvent.CLICK,this.clickedMe,false,0,true);
}
Code for ImageItemNoCache:
public class ImageItemNoCache extends ImageItem
{
private var textData:Object;
private var itemData:Object;
private var pathToItemsFolder:String;
public function ImageItemNoCache(param1:int, param2:int, param3:int, param4:int, param5:Object = null, param6:Object = null, param7:String = ".")
{
this.textData = param5;
this.itemData = param6;
this.pathToItemsFolder = param7;
super(param1,param2,param3,param4);
}
override protected function getImageItemData(param1:int, param2:int = -1, param3:int = -1, param4:int = -1) : ImageItemData
{
return new ImageItemDataNoCache(param1,param2,param3,param4,this.textData,this.itemData,this.pathToItemsFolder);
}
override public function clone() : DisplayItem
{
return new ImageItemNoCache(getType(),getOrdinal(),getId(),getInventoryType(),this.textData,this.itemData,this.pathToItemsFolder);
}
}

Error #1009 sometimes during game

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

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 removeChild - I cannot target specific children

I'm still trying to get my head around AS3 and can't figure out how to target some specific Children in my project and remove them... I'm sure there's a simple solution to this on page 1 of a "Basic AS3" book somewhere but I can't work it out!
I'm trying to remove the red lines created in the addNewPoint function (or just give them an alpha of 0) as part of the fillDriveway function.
I can give you a link to the source files if you need.
Thanks so so much in advance!
Here's my terrible code:
import flash.display.*
pic.addEventListener(MouseEvent.CLICK,addNewPoint);
var n:Number = 0;
function addNewPoint(e:MouseEvent):void {
n++;
pointNo.text = String(n);
var nextPoint:MovieClip = new newPoint();
addChild(nextPoint);
nextPoint.name = "mc"+pointNo.text;
nextPoint.x = e.target.mouseX;
nextPoint.y = e.target.mouseY;
var joinPoints:MovieClip = new MovieClip();
this.addChild(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);
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);
}
}
}
undo.addEventListener(MouseEvent.CLICK, undoit);
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 Actions
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);
}
//BTN Calls
btn1.addEventListener(MouseEvent.CLICK, fillDriveway);
btn1.addEventListener(MouseEvent.ROLL_OVER, btn1over);
btn1.addEventListener(MouseEvent.ROLL_OUT, btn1out);
btn1.buttonMode = true;
btn1.useHandCursor = true;
btn2.addEventListener(MouseEvent.CLICK, maskDrive);
btn2.addEventListener(MouseEvent.ROLL_OVER, btn2over);
btn2.addEventListener(MouseEvent.ROLL_OUT, btn2out);
btn2.buttonMode = true;
btn2.useHandCursor = true;
btn3.buttonMode = true;
btn3.useHandCursor = true;
btn3.addEventListener(MouseEvent.ROLL_OVER, btn3over);
btn3.addEventListener(MouseEvent.ROLL_OUT, btn3out);
I think your issue is that each time you create a new joinPoints you are overwriting the pointer to the previous joinPoints instance, therefore you cannot access it. (this will create a memory leak).
Each time you create a joinPoints object, add it to an array:
joinPointsArray.push(joinPoints);
Your fillDriveway function can then access all the joinPoints you have created:
for (var i:int = 0; i < joinPointsArray.length, i++) {
joinPointsArray[i].alpha = 0;
}

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