PDA

View Full Version : [AS3] odd SimpleButton problem in AS3


dna
07-02-2007, 12:33 AM
I'm really missing something here. I've created this code (adapted from the AS3 Cookbook) so that I could work on learning how to do FocusManager stuff. However, I've encountered a problem I cannot figure out. The code creates 3 Simple buttons, each identical, with functions to create the (identical) upstate, overstate and downstate for each. The hit test state is assigned for each to be the same as the overstate. A simple mouse event traces "Mouse clicked the button" when each button is clicked.

For some reason that I totally cannot figure out, there is an instance of the downState drawn up in the top left corner. I've tried commenting out all references to the downstate and this instance remains, but is changed to an instance of the upstate.

Odder still, if I click all three buttons in any order (but must be all three) the random floating button state disappears. I have been sitting and staring at this for quite a while. The code is so simple, and is throwing no errors. Where is this ghost button coming from?

I've attached the .fla.

Thanks in advance, if it's a stunningly simple flaw somewhere I'll be more relieved than embarassed...;) dina



var leftbutton:SimpleButton = new SimpleButton();
leftbutton.x = 50;
leftbutton.y = 300;
leftbutton.upState = createUpState();
leftbutton.overState = createOverState();
leftbutton.downState = createDownState();
leftbutton.hitTestState = leftbutton.upState;
leftbutton.addEventListener(MouseEvent.CLICK, handleClick);


var middlebutton:SimpleButton = new SimpleButton();
middlebutton.x = 200;
middlebutton.y = 300;
middlebutton.upState = createUpState();
middlebutton.overState = createOverState();
middlebutton.downState = createDownState();
middlebutton.hitTestState = middlebutton.upState;
middlebutton.addEventListener(MouseEvent.CLICK, handleClick);


var rightbutton:SimpleButton = new SimpleButton();
rightbutton.x = 350;
rightbutton.y = 300;
rightbutton.upState = createUpState();
rightbutton.overState = createOverState();
rightbutton.downState = createDownState();
rightbutton.hitTestState = rightbutton.upState;
rightbutton.addEventListener(MouseEvent.CLICK, handleClick);



function createUpState():Sprite {

var sprite:Sprite = new Sprite;
sprite.graphics.lineStyle(8, 0xFF0000);
sprite.graphics.beginFill(0x990000);
sprite.graphics.drawRoundRect(0,0,130,86,20);
sprite.graphics.endFill();

addChild(sprite);
return sprite;

}

function createOverState():Sprite {
var sprite:Sprite = new Sprite;
sprite.graphics.lineStyle(8, 0x66FF33);
sprite.graphics.beginFill(0x990000);
sprite.graphics.drawRoundRect(0,0,130,86,20);
sprite.graphics.endFill();

addChild(sprite);
return sprite;
}

function createDownState():Sprite {
var sprite:Sprite = new Sprite;
sprite.graphics.lineStyle(8, 0xFF0000);
sprite.graphics.beginFill(0x999999);
sprite.graphics.drawRoundRect(0,0,130,86,20);
sprite.graphics.endFill();

addChild(sprite);
return sprite;
}


addChild(leftbutton);
addChild(middlebutton);
addChild(rightbutton);

function handleClick(event:MouseEvent):void
{

trace("Mouse clicked the button");
}

Mazoonist
07-02-2007, 03:44 AM
Inside of the three functions:
createUpState, createOverStage, and createDownState, delete (or comment out) the line that says:

addChild(sprite);

(leave the one that says return sprite, though).

What it's doing is adding a copy of that particular sprite that it just created to the stage. Since x and y aren't specified, it defaults to the upper left hand corner, where the three sprites stack one on top of another. I'm not advanced enough to know why they disappear after all three buttons are clicked, though. But I suspect that the authors of the cookbook threw that in there on purpose for a demonstration of some principle that I don't understand yet. I haven't messed with SimpleButton yet, so that block of code is very instructive to me at this point. I'm soaking it all up like a sponge! Yeah, right....

Dang, I knew I shoulda got the Actionscript Cookbook!

Edit: I shoulda read your post more carefully. You said you adapted the code, so you might be the source of the extra "addChild" in it.

dna
07-02-2007, 03:15 PM
There were a few versions of simplebuttons in the cookbook and some had addChild statements, but on closer look, they were only adding children of the sprite (text, etc.) It makes sense now I think -- the sprites only need to be returned in the function because when the buttons are added to stage they go with them. I am finding the cookbook very useful, the only slightly annoying thing is when you have a project idea and they have an answer that is close, but not quite there. dina

bot66
08-12-2008, 10:38 PM
Thanks! you just solved the problem I was having as well