remove and addchild button in as3.0 - actionscript-3

I want to click on the button "choose1" then button will disappear and the next is to add a button.
I would like to make two requests in a click event of the button "choose1".
EDIT: this is my code
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.display.Stage;
public class Bo extends MovieClip
{
var choose1:Choose;
public function Bo()
{
// constructor code
choose1=new Choose();
addChild(choose1);
choose1.addEventListener(MouseEvent.CLICK,clickChoose);
}
function clickChoose(e:MouseEvent):void
{
for (var i:Number=stage.numChildren -1; i>=0; i--)
{
stage.removeChildAt(i);
//stage.getChildAt(i).visible = false;
}
var ex:Next=new Next();// add new button
addChild(ex);
//this.stage.removeEventListener(MouseEvent.CLICK,clickChoose);
}
}
anyone can help me? thanks.

Removing a button after click is bad UX, consider adding whichever button you want but keeping the old one.

Related

Having trouble with my Flash Comic

I'm making a webcomic in Adobe Animate right now, and the way a viewer can turn the "page" is by clicking on the screen.
First, I put a code on one frame shown as follows:
import flash.events.KeyboardEvent;
import flash.events.MouseEvent;
import flash.ui.Keyboard;
stop();
stage.addEventListener(MouseEvent.CLICK,forward2);
stage.addEventListener (KeyboardEvent.KEY_DOWN,forward);
function forward2 (event:MouseEvent) :void {
gotoAndStop(currentFrame+1);
}
function forward (e:KeyboardEvent): void {
if (e.keyCode == Keyboard.RIGHT)
gotoAndStop(currentFrame+1);
}
Then, if I wanted a scene to play out without the viewer having to click on the screen, I wrote another code:
import flash.events.KeyboardEvent;
import flash.events.MouseEvent;
import flash.ui.Keyboard;
stop();
stage.addEventListener(MouseEvent.CLICK,forward4);
stage.addEventListener (KeyboardEvent.KEY_DOWN,forward3);
function forward4 (event:MouseEvent) :void {
gotoAndPlay(currentFrame+1);
}
function forward3 (e:KeyboardEvent): void {
if (e.keyCode == Keyboard.RIGHT)
gotoAndPlay(currentFrame+1);
}
So because the function was already defined on another frame, I posted this code on the frame after the animation:
import flash.events.KeyboardEvent;
import flash.events.MouseEvent;
import flash.ui.Keyboard;
stop();
stage.addEventListener(MouseEvent.CLICK,forward2);
stage.addEventListener (KeyboardEvent.KEY_DOWN,forward);
I tried to make sure that the script was constant throughout the frames I wanted it to be used for by inserting a blank frame for each frame I wanted it to be active on. However, when I tested it, as soon as I clicked on the frame I put the code on (The one without the function defined), it just sent me back to the beginning.
What am I doing wrong?
When you add a listener to any object it will continue to exist until it is explicitly removed.
Before going to the next frame you should run:
stage.removeEventListener(MouseEvent.CLICK, forward2);
stage.removeEventListener(KeyboardEvent.KEY_DOWN, forward);
This will remove the event listeners and prevent forward2 and forward from running when you click or press a key on some other frame.

Unable to addChild on MouseCoordinations

What i am trying to do is, to place a new tower each time on MouseX and MouseY. but it seems it isn't working Any Idea guys?
or if you can create a tileMap array and add new child of PrototypeTower each time when we click on the Tower (on x=50, y=400) to select and place wherever we want. When i click the the box at x=50, y=400, the startDrag() doesn't work.
Main.as
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.ui.Mouse;
public class Main extends Sprite
{
private var pTower:PrototypeTower = new PrototypeTower;
private var zTower:PrototypeTower = new PrototypeTower;
private var fTower:PrototypeTower = new PrototypeTower;
public function Main()
{
pTower.x = 50;
pTower.y = 400;
addChild(pTower);
addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
}
private function onAddedToStage(e:Event):void
{
removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
pTower.addEventListener(MouseEvent.CLICK, onClicked);
}
private function onClicked(e:Event):void
{
removeEventListener(MouseEvent.CLICK, onClicked);
zTower.x = mouseX;
zTower.y = mouseY;
addChild(zTower);
zTower.startDrag();
addEventListener(MouseEvent.CLICK, onPlaced);
}
private function onPlaced(e:Event):void
{
removeEventListener(MouseEvent.CLICK, onPlaced);
zTower.stopDrag();
fTower.x = mouseX
fTower.y = mouseY;
addChild(fTower);
}
}
}
PrototypeTower.as
package
{
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.events.Event;
public class PrototypeTower extends MovieClip
{
public function PrototypeTower()
{
this.graphics.beginFill(0x00FF00);
this.graphics.drawRect(this.x, this.y, 20, 20);
this.graphics.endFill();
}
}
}
Thank you, i am totally noob, wondering around from days!
Sorry this is just a schematic for now. I'll try to refine it later if you need. Hopefully this clears some things up for you though
When you click the tower, add a new tower at the x,y of the mouse and do some sort of startDrag(tower) on the new tower you just added. Then when you click on the stage, stop the drag and set the new X,y
edit
You might try getting rid of all your removeEventListener calls
I think your problem is you are removing the event listener that gets added in your main function. Keep in mind that the main function (class constructor) for the document class only gets called once, so you add an event listener for ADDED_TO_STAGE only once, and then you remove it the first time you try to add anything to the stage, and you never put it back. Just don't remove it in the first place.
You are calling zTower.startDrag() before you add it to the stage. Do addChild(zTower) and then zTower.startDrag()

Custom Button is not being displayed

I am attempting to display a class derived from SimpleButton on a Flash screen. This button is supposed to load an external JPG file and use it as the display.
The code for the custom button is shown below:
package {
import flash.display.Sprite;
import flash.display.Loader;
import flash.net.URLRequest;
public class CustomState extends Sprite {
public function CustomState()
{
var loader:Loader = new Loader();
loader.load(new URLRequest("file.jpg"));
this.width = 70;
this.height = 100;
addChild(loader);
trace("State Width: "+this.width);
}
}
}
The button state code is shown below:
package {
import flash.display.Sprite;
import flash.display.Loader;
import flash.net.URLRequest;
public class CustomState extends Sprite {
public function CustomState()
{
var loader:Loader = new Loader();
loader.load(new URLRequest("file.jpg"));
this.width = 70;
this.height = 100;
addChild(loader);
trace("State Width: "+this.width);
}
}
}
The code used to test the button is below:
package {
import flash.display.MovieClip;
public class TestButton extends MovieClip{
public function TestButton()
{
var button:CustomButton = new CustomButton();
button.x = 10;
button.y = 30;
addChild(button);
}
}
}
For reasons that are not clear, the button does not appear at all.
Can someone tell me how to make this button appear? As can be seen from the test class, I am adding the button to its display list. I also note that despite the fact that I am setting width and height for the sprite, its width and height apparently aren't being set. Something funky is going on, but my attempts to find a piece of working code that does what I am trying to do have failed.
Someone please advise...

How can I stopped all layers in layers movie clip when click pause button using action script-3?

I have use action script 3 but problem is, when I click stop button then stop just object & graphic not a pause movie clip, so how can I pause full screen ?
If you want to stop all MovieClips,try this
import flash.display.MovieClip;
import flash.display.DisplayObject;
import flash.display.DisplayObjectContainer;
stopAll(stage);
private function stopAll(obj:DisplayObject):void {
var mc:MovieClip = obj as MovieClip;
var container:DisplayObjectContainer = obj as DisplayObjectContainer;
if (mc) {
mc.stop();
}
if (container) {
for each (var child:DisplayObject in container) {
stopAll(obj);
}
}
}

AS3 Undefined Method for button class

I'm trying to make a button link to an external page. In the first frame I placed this code:
import flash.events.MouseEvent;
import flash.net.navigateToURL;
import flash.net.URLRequest;
import flash.net.URLRequestMethod;
myButton.addEventListener(MouseEvent.CLICK, onMouseClick);
function onMouseClick(event:MouseEvent):void
{
var request:URLRequest = new URLRequest("http://pruebainteligente.com");
request.method = URLRequestMethod.GET;
var target:String = "_blank";
navigateToURL(request, target);
}
To make the button class I went to the library > properties > And named the button Class "myButton".
What I'm doing wrong?
myButton.addEventListener(MouseEvent.CLICK, onMouseClick);
If that code is on the frame itself it is referring to an instance of an object/symbol, not the class itself. You will need to drag your button from the library and place it onto the stage, set its instance name (in the properties tab) to "myButton", then your code will work.
Alternatively you can add a copy of your object purely through code:
var newButton = new myButton();
addChild(newButton);
newButton.addEventListener(MouseEvent.CLICK, onMouseClick);