Child not added, when check index it shows -1 - actionscript-3

I am not sure why i addedChild but when i check it's index it still shows -1.
What i am trying to do is:
1.)Loop a URLRequest and load bitmap pictures.
2.)Put them in individual _contentHolder
3.)Put everything in a viewport
4.)Check the index of the image when its clicked
5.)Display just the picture that is clicked(With a black background) (Picture viewer)
6.)When the picture or background is clicked again, it closes the "picture viewer"(The single picture with black background), and just display the list from before.
At the moment i can upload the pictures in a loop, then add them into a viewport, but i can not managed to get the index of the image and reload it.
Thanks for your time!
Code:
public var _contentHolder:Sprite = new Sprite;
public var _contentHolder1:Sprite;
public var loadedArray:Array = new Array;
public var blackBox:Sprite = new Sprite();
private var somedata:Array;
protected var Holder:Listing9 = new Listing9;
public var viewport:Viewport = new Viewport();
public var scroller:TouchScroller = new TouchScroller();
var my_url:Array = somedata;
function loadImage():void
{
somedata = SearchVectorTest.lists;
for (var i:int = 5; i < somedata.length; i++)
{
if (somedata[i])
{
var loader:Loader = new Loader();
loader.load(new URLRequest("http://www.rentaid.info/rent/" + somedata[i]));
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onImageLoaded);
}
}
}
function onImageLoaded(e:Event):void
{
loadedArray.push(e.currentTarget.loader.content as Bitmap);
for (var i:int = 0; i < loadedArray.length; i++)
{
var currentY1:int = 200;
e.currentTarget.loader.content.height = 200;
e.currentTarget.loader.content.y += currentY1;
currentY1 += e.currentTarget.loader.content.height + 300;
_contentHolder.mouseChildren = false; // ignore children mouseEvents
_contentHolder.mouseEnabled = true; // enable mouse on the object - normally set to true by default
_contentHolder.useHandCursor = true; // add hand cursor on mouse over
_contentHolder.buttonMode = true;
_contentHolder.addChild(loadedArray[i]);
}
var viewport:Viewport = new Viewport();
viewport.y = 0;
viewport.addChild(_contentHolder);
var scroller:TouchScroller = new TouchScroller();
scroller.width = 300;
scroller.height = 265;
scroller.x = 10;
scroller.y = 100;
scroller.viewport = viewport;
addChild(scroller);
_contentHolder.addEventListener(MouseEvent.CLICK, gotoscene);
}
loadImage();
public function gotoscene(e:MouseEvent):void
{
var searchString = loadedArray;
var index:Number;
index = searchString.indexOf(e.target);
trace(index);
trace(_contentHolder);
trace(_contentHolder.parent);
blackBox.graphics.beginFill(0x000000);
blackBox.graphics.drawRect(-1, -1, stage.width, stage.height);
blackBox.alpha = 0.7;
addChild(blackBox);
Holder.height = 300;
Holder.width = stage.width;
Holder.x = 0;
Holder.y = 100;
trace(blackBox);
trace(blackBox.parent);
addChild(Holder);
}
function gotoscene1(e:MouseEvent):void
{
removeChild(Holder);
removeChild(blackBox);
}

The indexOf you are using strict equality (===). e.target is of type Sprite and your array is filled with Bitmaps.
The strict equality will fail because the objects are not of the same type.
You need to use items of the same type to make the comparison succeed.

Related

Dynamic Gallery to show download and progress bar in as3

My flash project has specific task to show the dynamic gallery items based on an XML list and there is a download option available for each gallery item.
For this I made a movieclip (imageTile) with a Thumbnail, Title, ProgressBar & ProgressText as shown below.
I have two classes named Main.as and FileRef.as
Main.as
var tileMap:Dictionary = new Dictionary();
public var tile:ImageTile;
addChild(wall);
wallWidth = wall.width;
wallHeight = wall.height;
var columns:Number;
var my_x:Number;
var my_y:Number;
var my_thumb_width:Number;
var my_thumb_height:Number;
var images:XMLList;
var total:Number;
var swipe:Number = 0;
var myXMLLoader:URLLoader = new URLLoader();
myXMLLoader.load(new URLRequest("gallery.xml"));
myXMLLoader.addEventListener(Event.COMPLETE, processXML);
function processXML(e:Event):void {
myXML = new XML(e.target.data);
images = myXML.IMAGE;
total = images.length();
myXMLLoader.removeEventListener(Event.COMPLETE, processXML);
myXMLLoader = null;
var loader:Loader;
for (var i:uint = 0; i < total; i++) {
tile = new ImageTile();
wall.addChild(tile);
tile.x = i % 3 * 400 + 180;
tile.y = Math.floor(i / 3) * 650 + 250;
var imageName:String = images[i].#FULL;
path = images[i].#Path;
var title:String = images[i].#Title;
**var caption:TextField = tile.getChildByName("caption_tf") as TextField;**
caption.text = title;
tile.addEventListener(MouseEvent.CLICK, onTileClick(path));
loader = new Loader();
loader.load(new URLRequest("images/" + imageName));
tileMap[loader] = tile;
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onImageLoad);
}
//trace(myXML);
}
var tileMap:Dictionary = new Dictionary();
function onImageLoad(e:Event):void {
var loader:Loader = e.target.loader;
var tile:ImageTile = tileMap[loader] as ImageTile;
var image:Bitmap = loader.content as Bitmap;
image.x = -100;
image.y = -100;
image.width=366;
image.height=418;
var textField:DisplayObject = tile.getChildByName("caption_tf");
var textFieldDepth:int = tile.getChildIndex(textField);
tile.addChildAt(image, textFieldDepth);
tileMap[loader] = null;
image.smoothing = true;
}
function onTileClick(url:String):Function {
return function(me:MouseEvent):void {
path = url;
var download:FileRef = new FileRef();
download.downloadFile(path);
}
FileRef.as code
public var mc_loaded : MovieClip = Main.gameInstance.tile.getChildByName("mc_loaded") as MovieClip,
mc_progress : MovieClip = Main.gameInstance.tile.getChildByName("mc_progress") as MovieClip,
txt_prog : TextField = Main.gameInstance.tile.getChildByName("txt_prog") as TextField;
public function downloadFile(url:String) : void
{
/// Download the gallery item codes using url
}
public function progressHandler( event : ProgressEvent ) : void
{
//mc_loaded.scaleX = (event.bytesLoaded / event.bytesTotal) ;
}
public function completeHandler( event : Event ) : void
{
//reset progress bar after download is finished
mc_loaded.scaleX = 0; // I want to use the imageTile element
txt_prog.text = "download finished";
}
public function OnZipComplete(evt:flash.events.Event):void
{
txt_prog.text = "download finished";
}
The download works fine, but I can not get the progress bar and progress text for every tile created.
I got the answer
Remove onTileClick(url:String) function from Main.as
Main.as
function processXML(e:Event):void {
//// Codes /////
//// Codes /////
var download:FileRefTut = new FileRefTut();
tile.addEventListener(MouseEvent.CLICK, download.downloadFile(path));
}
FileRef.as
public var txt_prog:TextField, mc_loaded:MovieClip;
public function downloadFile(url:String):Function {
return function(me:MouseEvent):void {
var tile:ImageTile = me.currentTarget as ImageTile;
txt_prog = tile.getChildByName("txt_prog") as TextField;
mc_loaded = tile.getChildByName("mc_loaded") as MovieClip;
}
public function progressHandler( event : ProgressEvent ) : void
{
mc_loaded.scaleX = (event.bytesLoaded / event.bytesTotal) ;
}
public function completeHandler( event : Event ) : void
{
//reset progress bar after download is finished
mc_loaded.scaleX = 0; // I want to use the imageTile element
txt_prog.text = "download finished";
}
public function OnZipComplete(evt:flash.events.Event):void
{
txt_prog.text = "download finished";
}

Dynamically adding a 3 tier movieclip to stage then populate it

What I am trying to do is add say 20 jpgs labelled 1-20 individually inside movieClips [ sReel ] , then add the movieClips to movieClip [ aReel ] and duplicate movieClip [ aReel ] into movieClip [ mReel ] so I can have a horizontal scrolling movieClip.
I can do all manually no problem but doing it automatically is not so easy :(
Just need how to stack the mocieClips correctly as know how to do rest oc code Thanks..
var movie_Number:Array = new Array();
var movie_Title: Array = new Array();
var movie_Director: Array = new Array();
var movie_Star: Array = new Array();
var movie_Duration: Array = new Array();
var movie_Genre: Array = new Array();
var movie_Year: Array = new Array();
var movie_Rating: Array = new Array();
var movie_Comment: Array = new Array();
var mReel:MovieClip = new MovieClip;
var aReel:MovieClip = new MovieClip;
var bReel:MovieClip = new MovieClip;
var sReel:MovieClip = new MovieClip;
var ldr:Loader;
newReelMask.cacheAsBitmap = true;
mReel.cacheAsBitmap = true;
mReel.mask = newReelMask;
var xmlLoader:URLLoader = new URLLoader(), xmlData:XML;
var movieNum:Number;
var xReel = 0;
mReel.addEventListener(MouseEvent.MOUSE_OVER, onStop);
mReel.addEventListener(MouseEvent.MOUSE_OUT, onStart);
var myTimer:Timer = new Timer(1);
myTimer.addEventListener(TimerEvent.TIMER, timerListener);
myTimer.start();
init();
makeSmallReel();
aReel.x = 0;
aReel.y = 0;
mReel.addChild(aReel);
addChild(mReel);
function init():void
{
xmlLoader.addEventListener(Event.COMPLETE, LoadXML);
xmlLoader.load(new URLRequest("movieInfo.xml"));
}
function LoadXML(e:Event):void
{
xmlData = new XML(e.target.data);
var movieTitle:XMLList = xmlData..title;
var movieDirector:XMLList = xmlData..director;
var movieStar:XMLList = xmlData..star;
var movieDuration:XMLList = xmlData..duration;
var movieGenre:XMLList = xmlData..genre;
var movieYear:XMLList = xmlData..year;
var movieRating:XMLList = xmlData..rating;
var movieComment:XMLList = xmlData..comment;
var len: int = xmlData.movie.length();
var i:int = 0;
for (i; i < len; i++)
{
movie_Number.push(i);
movie_Title.push(movieTitle[i]);
movie_Director.push(movieDirector[i]);
movie_Star.push(movieStar[i]);
movie_Duration.push(movieDuration[i]);
movie_Genre.push(movieGenre[i]);
movie_Year.push(movieYear[i]);
movie_Rating.push(movieRating[i]);
movie_Comment.push(movieComment[i]);
}
}
function makeSmallReel():void
{
var i:int = 1;
for (i; i < 11; i++)
{
ldr = new Loader();
ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, ldr_complete);
ldr.load(new URLRequest("images/thumb/"+i+".jpg"));
}
}
function ldr_complete(e:Event):void
{
sReel.x = xReel;
sReel.addChild(ldr);
xReel = xReel + e.target.width;
aReel.addChild(sReel);
}
function timerListener(e:TimerEvent):void
{
if (mReel.x <= -(aReel.width))
{
mReel.x = 0;
}
mReel.x -= 1;
}
function onStop(e:MouseEvent):void
{
myTimer.stop();
}
function onStart(e:MouseEvent):void
{
myTimer.start();
}
You 're adding always the same loader(ldr is a class-var) to your container.
At loading you have to create a ldr vor each picture. And add this to your container in your complete-handler. (it's e.target in your complete-handler)
BTW: You don't need a MovieCLip. A Sprite is enought, 'cause semms, you never need the timeline of a movieclip, which is the difference between MovieClip and Sprite ( and the overhead).

Actionscript3 Clone and Drag object over other

I am building a simple flash game where user can drag objects from a tool-box and drag over to an area. On dragging the object, I create a clone of that object and place it over the area. Here is my code that works somewhat fine..
package {
imports...
public class DocumentClass extends MovieClip {
//variables...
var someArray:Array = new Array();
var totalObjs = 5;
var objOnStage:Array = new Array();
var ogx;
var ogy;
public function DocumentClass() {
// constructor code
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
_width = stage.stageWidth;
_height = stage.stageHeight;
setGameObjects();
}//constructor close
//scaling Functions
public function scalingheight(clip:MovieClip, percent:Number){
var ratio = clip.width / clip.height;
clip.height = percent * _height;
clip.width = clip.height * ratio;
}
public function scalingwidth(clip:MovieClip, percent:Number){
var ratio = clip.height / clip.width;
clip.width = percent * _width;
clip.height = clip.width * ratio;
}
public function setGameObjects():void{
mc_toolbox = new mctoolbox;
addChild(mc_toolbox);
mc_toolbox.x = mc_toolbox.y = 0;
scalingwidth(mc_toolbox, 0.08);
mc_toolbox.height = _height;
mc_board = new mcboard;
addChild(mc_board);
mc_board.x = mc_toolbox.width;
mc_board.y = 0;
scalingwidth(mc_board, 0.75);
mc_board.height = _height;
mc_optbox = new mcoptbox;
addChild(mc_optbox);
scalingwidth(mc_optbox, 0.10);
mc_optbox.height = _height;
mc_optbox.x = _width - mc_optbox.width;
mc_optbox.y = 0;
setobjects();
}
public function setobjects():void{
for(var i = 1; i <= totalObjs; i++){
var className:String = "obj" + i;
var ClassReference:Class = getDefinitionByName(className) as Class;
var myInstance = new ClassReference;
addChild(myInstance);
scalingwidth(myInstance, 0.08);
someArray.push(myInstance);
myInstance.x = stage.stageWidth - 0.09 * _width;
myInstance.y = myInstance.height * (i-1);
}
for(var i = 1; i <= totalObjs; i++){
someArray[i-1].addEventListener(MouseEvent.MOUSE_DOWN, startMove);
someArray[i-1].addEventListener(MouseEvent.MOUSE_UP, stopMove);
}
}
function startMove(evt:MouseEvent):void {
//clone your movieclip here
ogx = evt.currentTarget.x;
ogy = evt.currentTarget.y;
evt.currentTarget.startDrag();
}
function stopMove(evt:MouseEvent):void {
var newobj = new evt.currentTarget.constructor;
addChild(newobj);
newobj.width = evt.currentTarget.width;
newobj.height = evt.currentTarget.height;
newobj.x = evt.currentTarget.x;
newobj.y = evt.currentTarget.y;
evt.currentTarget.x = ogx;
evt.currentTarget.y = ogy;
evt.currentTarget.stopDrag();
objOnStage.push(newobj);
}
}//class close
}//package close
My problem arises when I drag one object on the area and then I drag one more on the previous object. In such case the mouse up event is not called and hence the object movieclip does not clone itself. I think I am making some silly mistake, please guide.
I got this solved. The second movieclip came behind the first so I just brought it in front of all other movieclips while dragging using this
function startMove(evt:MouseEvent):void {
//clone your movieclip here
ogx = evt.currentTarget.x;
ogy = evt.currentTarget.y;
var myobj = evt.currentTarget as DisplayObject;
setChildIndex(myobj, numChildren - 1); // < The solution
evt.currentTarget.startDrag();
}
Try, removing MouseEvent.MOUSE_UP after dropping the object on the stage like so,
evt.currentTarget.removeEventListener(MouseEvent.MOUSE_UP, stopMove);

not currently clicked movieclip comes up as true

private var previousClick = null;
public static var floor1:Array = new Array();
floor1[0] = [2,1,1,1,1,1,2];
floor1[1] = [1,1,1,1,1,1,1];
floor1[2] = [1,1,1,2,1,1,1];
floor1[3] = [1,1,1,1,1,1,1];
floor1[4] = [1,1,1,2,1,1,1];
floor1[5] = [1,1,1,1,1,1,1];
floor1[6] = [2,1,1,1,1,1,2];
public function Main()
{
var tilew:int = 60;
var tileh:int = 60;
for (var i:int=0; i<floor1.length; i++)
{
for (var u:int=0; u<floor1[i].length; u++)
{
var cell:MovieClip = new Tile();
cell.gotoAndStop(floor1[i][u]);
cell.x = ((u-i)*tileh)+365;
cell.y = ((u+i)*tileh/2)+70;
addChild(cell);
cell.addEventListener(MouseEvent.CLICK, mouseclick);
}
}
function mouseclick(event:MouseEvent)
{
(event.currentTarget as Tile).outline.gotoAndStop("active");
event.currentTarget.cellSelected = true;
event.currentTarget.removeEventListener(MouseEvent.CLICK,mouseclick);
if (previousClick !== null){
(previousClick as Tile).outline.gotoAndStop(1);
event.currentTarget.cellSelected = false;
previousClick.addEventListener(MouseEvent.CLICK,mouseclick);
trace(previousClick);
}
previousClick = event.currentTarget;
previousClick.cellSelected = true;
}
So I have tiles called cells lined up to each other to make a floor. When you click a cell, the outline of that tile glows, indicating that it is currently selected. I have also created a boolean that checks if the current tile is selected. The problem is, the boolean is returning true to a tile that I clicked that isn't the currently selected one, especially the first tile I click when the game starts.
So my question is, how can I get a tile that I clicked that isnt currently selected to not equal true?
I think it caused by overlap.
you may try cell.y = ((u+i)*tileh)+70;

How do I remove all instances called by the AddChild function?

I'm working on a sniper game and I'm trying to fix a issue I run into when I click the character to kill him. Anyways, long sotry short multiple instances of a MovieClip are created and I need them all to be removed at the same time when a if statement is executed. Is this possible, if so, what is the code?
Code:
stop();
Mouse.hide();
var blood:Array = [];
// after you create your clips your needing to keep track of.
var level_complete1:level_complete = new level_complete();
var ammo:Number = 5;
var cash:Number= 100;
var level:Number = 1;
var exp:Number = 0;
var blood_c:Number = 0;
var exp_needed = 25;
var dead_check:Number = 0; //Check to see if the target is dead
var check_blood:Number = 0; //Check to make sure blood doens't loop
var blood_splat1:blood_splat = new blood_splat();
blood.push(blood_splat1);
target1.addEventListener(MouseEvent.CLICK, target_shot);
function target_shot(event:MouseEvent):void{
if(dead_check==0){
addChild(blood_splat1);
blood_splat1.y = mouseY;
blood_splat1.x = mouseX;
target1.gotoAndPlay(32);
if (blood_splat1.currentFrame==6){
//blood_splat1.gotoAndStop(6);
//removeChild(blood_splat1);
cash=cash+150;
exp=exp+25;
ammo=ammo-1;
dead_check = 1;
blood_c = 1;
}
}
if (dead_check==1){
addChild(blood_splat1);
blood_splat1.y = mouseY;
blood_splat1.x = mouseX;
target1.gotoAndStop(38);
blood_c = 1;
//if (blood_splat1.currentFrame==6){
//blood_splat1.gotoAndStop(6);
if (blood_c==1){
for each(var mc:MovieClip in blood){
mc.parent.removeChild(mc);
blood.splice(blood.indexOf(mc), 1);
}
}
}
}
Edit:
Ok here is the problem I see from testing.
Your never getting blood_c set to 1 so it will never remove the blood splat. if you force it (for testing I forced it to = 1) you will never see the blood splat because it is removed instantly after it is created.
So my advice is when you first addChild(blood_splat1); you make a timer. or a frame counter. or something and when it counts down or fires (like a timer it calls a function to remove the blood splat. Like this:
stop();
Mouse.hide();
import flash.utils.Timer;
import flash.events.TimerEvent;
var bloodTimer:Timer = new Timer(1000, 1); // one second and doesn't repeat.
//var level_complete1:level_complete = new level_complete();
var ammo:Number = 5;
var cash:Number= 100;
var level:Number = 1;
var exp:Number = 0;
var blood_c:Number = 0;
var exp_needed = 25;
var dead_check:Number = 0; //Check to see if the target is dead
var check_blood:Number = 0; //Check to make sure blood doens't loop
var blood_splat1:blood_splat = new blood_splat();
target1.addEventListener(MouseEvent.CLICK, target_shot);
function removeBlood(te:TimerEvent):void
{
if(blood_splat1.parent != null && blood_splat1.parent.contains(blood_splat1))
{
blood_splat1.parent.removeChild(blood_splat1);
}
}
function target_shot(event:MouseEvent):void
{
if(dead_check==0)
{
addChild(blood_splat1);
bloodTimer.start();
bloodTimer.addEventListener(TimerEvent.TIMER_COMPLETE, removeBlood);
blood_splat1.y = mouseY;
blood_splat1.x = mouseX;
target1.gotoAndPlay(32);
if (blood_splat1.currentFrame==6)
{
//blood_splat1.gotoAndStop(6);
//removeChild(blood_splat1);
cash=cash+150;
exp=exp+25;
ammo=ammo-1;
dead_check = 1;
blood_c = 1;
}
}
if (dead_check==1)
{
addChild(blood_splat1);
bloodTimer.start();
bloodTimer.addEventListener(TimerEvent.TIMER_COMPLETE, removeBlood);
blood_splat1.y = mouseY;
blood_splat1.x = mouseX;
target1.gotoAndStop(38);
blood_c = 1;
//if (blood_splat1.currentFrame==6){
//blood_splat1.gotoAndStop(6);}
}
}
Down and dirty, you can do this (but it won't remove children of the children) :
while (numChildren > 0) {
removeChild(getChildAt(0));
}