Log in

View Full Version : Button troubles.



punstc
09-08-2008, 10:27 PM
I'm not sure whats wrong but, i have two buttons that aren't showing up as buttons. I have buttonMode turned on and an eventlistener listening for their click. I put a trace inside the function to trace out when they're clicked but nothing is happening. Can some one take a look and see if they can see any errors.

My buttons are called nButton and rButton and my function for them is nextImage and is at the very bottom of the code



stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;

var xml:XML;
var xmlLength:Number;
var xmlLoader:URLLoader;
var imgLoader:Loader;
var imgArray:Array = new Array();
var current:Number = 0;
var mc1:Sprite;
var mc2:Sprite;
var loaderMovie:Sprite;
var width1:Number;
var width2:Number;
var height1:Number;
var height2:Number;
var curMovie:Sprite;
var swap:int = 1;
var nButton:NextButton = new NextButton();
var rButton:ReverseButton = new ReverseButton();

nButton.x = stage.stageWidth - nButton.width;
nButton.y = stage.stageHeight/2 - nButton.height/2;
nButton.buttonMode = true;
nButton.mouseChildren = false;
nButton.addEventListener(MouseEvent.CLICK, nextImage);
rButton.y = stage.stageHeight/2 - rButton.height/2;
rButton.buttonMode = true;
rButton.mouseChildren = false;
rButton.addEventListener(MouseEvent.CLICK, nextImage);
addChild(nButton);
addChild(rButton);
loadGallery();

function loadGallery():void {
xmlLoader = new URLLoader();
xmlLoader.dataFormat = URLLoaderDataFormat.TEXT;
xmlLoader.addEventListener(Event.COMPLETE,portfolioLoaderOnComplete);
xmlLoader.addEventListener(IOErrorEvent.IO_ERROR,portfolioLoaderOnIoError);
xmlLoader.load(new URLRequest("images.xml"));

function portfolioLoaderOnComplete(e:Event):void {
xml = XML(e.target.data);
xmlLength = xml.*.length();
fillArray();
initBigMovies();
}
function portfolioLoaderOnIoError(e:IOErrorEvent):void {
xmlLoader = null;
trace("Error");
}
}

function fillArray():void {
for (var i:int = 0; i < xmlLength; i++) {
imgArray[i] = xml.image[i];
trace(imgArray[i]);
}
}

function initBigMovies():void {
mc1 = new Sprite();
mc2 = new Sprite();
mc1.name = "mc1";
mc2.name = "mc2";
loaderMovie = new Sprite();
addChild(mc1);
addChild(mc2);
addChild(loaderMovie);
loadBigImgs(current);
setProp();
}

function loadBigImgs(id:int):void {
id = current;
if (swap == 1) {
trace("LOADING IN MOVIE 1");
loadBigImg(imgArray[id],mc1);
curMovie = mc1;
swap = 2;
} else {
trace("LOADING IN MOVIE 2");
loadBigImg(imgArray[id],mc2);
curMovie = mc2;
swap = 1;
}
}

function setProp():void {
stage.addEventListener(Event.RESIZE,resized);
}

function resized(e:Event):void {
calc(mc1,width1,height1);
calc(mc2,width2,height2);
nButton.x = stage.stageWidth - nButton.width;
nButton.y = stage.stageHeight/2 - nButton.height/2;
rButton.y = stage.stageHeight/2 - rButton.height/2;
}

function loadBigImg(path,holder):void {
trace(path);
trace(holder);
loaderMovie.visible = true;
stage.mouseChildren = false;
loaderMovie.width = 1;
setChildIndex(loaderMovie,stage.numChildren -1);
imgLoader = new Loader();
imgLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,handleComplete);
imgLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS,handleProgress);
imgLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,handleIoError);
imgLoader.load(new URLRequest(path));

function handleProgress(e:ProgressEvent):void {
var percent:Number = e.bytesLoaded/e.bytesTotal;
loaderMovie.width = percent * stage.stageWidth;
}
function handleIoError(e:IOErrorEvent):void {
trace("ERROR LOADING IMAGE");
loaderMovie.visible = false;
stage.mouseChildren = true;
}
function handleComplete(e:Event) {
var bitmapData:BitmapData = new BitmapData(imgLoader.width,imgLoader.height,false);
bitmapData.draw(imgLoader.content,null,null,null,bitmapData.rect,true);
var bitmap:Bitmap = new Bitmap();
bitmap = new Bitmap(bitmapData,"auto",true);
bitmap.smoothing = true;
holder.addChild(bitmap);
if (curMovie == mc1) {
width1 = bitmap.width;
height1 = bitmap.height;
setChildIndex(mc1,stage.numChildren -1);
calc(mc1,width1,height1);
trace("FINISH LOADING IN MOVIE 1");
} else {
width2 = bitmap.width;
height2 = bitmap.height;
setChildIndex(mc2,stage.numChildren -1);
calc(mc2,width2,height2);
trace("FINISH LOADING IN MOVIE 2");
}
loaderMovie.width = stage.stageWidth;
setChildIndex(loaderMovie,stage.numChildren -1);
imgLoader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR,handleIoError);
imgLoader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS,handleProgress);
imgLoader.contentLoaderInfo.removeEventListener(Event.COMPLETE,handleComplete);
imgLoader = null;
}
}

function calc(mc,W,H) {
var obj1 = new Object();
var sW = stage.stageWidth;
var sH = stage.stageHeight;
var _loc2:Number = obj1.sCx = sW/W;
var sCy = obj1.sCy = sH/H;
if (_loc2<sCy) {
_loc2 = sCy;
}
obj1.wD = _loc2 * W;
obj1.hT = _loc2 * H;
obj1.X = obj1.Y = 0;
if (obj1.wD>sW) {
obj1.X = -Math.round((obj1.wD-sW)/2);
}
if (obj1.hT>sH) {
obj1.Y = -Math.round((obj1.hT-sH)/2);
}
mc.x = sW/2 - obj1.wD/2 ;
mc.y = sH/2 - obj1.hT/2 ;
mc.scaleX = obj1.wD/W*1;
mc.scaleY = obj1.hT/H*1;
}

function nextImage(e:MouseEvent) {
if (e.currentTarget == rButton) {
if (current >= 1) {
current --;
loadBigImgs(current);
trace("pressed rButton");
}
} else {
if (current < imgArray.length) {
current ++;
loadBigImgs(current);
trace("pressed nButton");
}
}
}



thanks for the help i appreciate it.

Medyman
09-09-2008, 02:31 AM
Try adding a trace before any of the if statements in nextImage() and see if it's being called. If that works, then your problem is with your internal conditionals.

If it doesn't work, then there is something on the stage that is preventing it from working.

punstc
09-09-2008, 02:44 AM
I found the problem a friend of mine pointed it out. i had a stage.mouseChildren = false on line 105 that was canceling everything out.

thanks for looking and taking the time i appreciate it

Medyman
09-09-2008, 04:56 PM
I found the problem a friend of mine pointed it out. i had a stage.mouseChildren = false on line 105 that was canceling everything out.

thanks for looking and taking the time i appreciate it


Ahh...I missed that. That would definitely do it though.

punstc
09-10-2008, 06:35 PM
thanks for taking the time though i really appreciate it, i looked through the file forever.