actionscript 3 add and remove movieclip button - actionscript-3

Trying to make buttons on actionscript: create and remove movieclip object, but always some errors...
I need one button creates movieclip, another must to delete it by mouse click. Here is code:
import flash.events.MouseEvent;
import flash.display.MovieClip;
import flash.text.TextField;
main.addEventListener(MouseEvent.CLICK, addMain);
function addMain(e: MouseEvent) {
var movieClip: bm = new bm();
movieClip.x = 640;
movieClip.y = 570;
this.addChild(movieClip);
main.removeEventListener(MouseEvent.CLICK, addMain);
deletemain.removeEventListener(MouseEvent.CLICK, addMain);
}
deletemain.addEventListener(MouseEvent.CLICK, deleteMain);
function deleteMain(e: MouseEvent) {
var movieClip: bm = bm(event.target);
this.removeChild(movieClip);
removeChild(movieClip);
movieClip = null;
deletemain.removeEventListener(MouseEvent.CLICK, deleteMain);
}

OK, to understand why your code is not working, let's take a look on it :
main.addEventListener(MouseEvent.CLICK, addMain);
function addMain(e: MouseEvent)
{
// here you are creating a local var "movieClip"
// inside your "addMain" function, so it's only accessible here
var movieClip:bm = new bm();
movieClip.x = 640;
movieClip.y = 570;
this.addChild(movieClip);
main.removeEventListener(MouseEvent.CLICK, addMain);
// I don't really know why you do this !
deletemain.removeEventListener(MouseEvent.CLICK, addMain);
}
deletemain.addEventListener(MouseEvent.CLICK, deleteMain);
function deleteMain(e: MouseEvent)
{
// here, you are creating a local var "movieClip"
// by casting (converting) your "deletemain" button (e.target) to an "bm" object
// event.target should be e.target
var movieClip: bm = bm(event.target);
// here your are removing the "deletemain" button, once
this.removeChild(movieClip);
// and twice
removeChild(movieClip);
movieClip = null;
deletemain.removeEventListener(MouseEvent.CLICK, deleteMain);
}
So to do what you want : creating a MovieClip and removing it using two buttons, you have to declare your "movieClip" as a global var to be accessible for your two listener functions :
// declare a global "bm" object
var movieClip:bm;
btn_add.addEventListener(MouseEvent.CLICK, btn_add_onPress);
function btn_add_onPress(e: MouseEvent): void
{
// create the "movieClip" object
movieClip = new bm();
addChild(movieClip);
// remove the click event listener for the btn_add
btn_add.removeEventListener(MouseEvent.CLICK, btn_add_onPress);
}
btn_delete.addEventListener(MouseEvent.CLICK, btn_delete_onPress);
function btn_delete_onPress(e:MouseEvent) : void
{
// remove the "movieClip" object
removeChild(movieClip);
movieClip = null;
// remove the click event listener for the btn_delete
btn_delete.removeEventListener(MouseEvent.CLICK, btn_delete_onPress);
}
Hope that can help.

Related

Test collision between two movie clips in two different classes AS3

I need to test the collision between 2 movie clips, using air for android action script 3.
Its a collision between an object and several obstacles.
My structure is the following :
The base FLA file, is linked to Action Script file called baseCode.as.
In this AS file, i create the obsctacles, using the following code :
baseCode.as :
package {
import flash.display.MovieClip;
import flash.events.Event;
import flash.geom.Point;
import Mc_MC; // Not strictly needed
public class baseCode extends flash.display.MovieClip
{
//private static var SYMBOLS:Array = new Array(MySymbol1, MySymbol2);
public var t:int = 0;
public function baseCode()
{
// Create five symbols:
for (var i:int = 0; i < 5; i++) {
trace(i);
makeSymbol();
}
}
function randomRange(minNum:Number, maxNum:Number):Number
{
return (Math.floor(Math.random() * (maxNum - minNum + 1)) + minNum);
}
public function makeSymbol():void
{
trace("IT entered makeSymbol");
// Pick a random symbol from the array:
// var symType:Class = SYMBOLS[0];
//trace(SYMBOLS[Math.random() * SYMBOLS.length]);
// Construct the new symbol:
//var Positi : Number = new Number(Math.random);
var loc:Point = new Point(randomRange(100,stage.stage.height),0);
var loc2:Point = new Point(randomRange(110,stage.stage.height),0);
//var loc:Point = new Point(10*randomRange(15, stage.width),100*randomRange(10 , stage.width));
trace("this is the starting point" , loc);
var sym:Mc_MC = new Mc_MC(1 + Math.random() *10, loc);
if( t % 2 == 0 ){
var sym2:Mc_MC2 = new Mc_MC2(15 + Math.random() *10, loc);
// Listen for the object hitting the left edge:
//sym2.addEventListener(Event.COMPLETE, remakeObject);
this.addChild(sym2);
}
sym.addEventListener(Event.COMPLETE, remakeObject);
this.addChild(sym);
t ++;
}
public function remakeObject(e:Event):void
{
e.target.removeEventListener(Event.COMPLETE, remakeObject);
//e.removeChild(sym);
//e.parent.removeChild(this.child);
// removeChild(this);
// this.removeChild(sym);
// Replace the dead symbol:
makeSymbol();
}
}
}
Mc_MC and Mc_MC2 are two Action Script file in which the obstacles are called :
Mc_MC.as :
package {
import flash.display.MovieClip;
import flash.events.Event;
import flash.geom.*;
import flash.display.Screen;
import flash.system.Capabilities;
public class Mc_MC extends MovieClip
{
public var speed:Number; // Pixels moved per frame
var valuee:baseCode;
public function Mc_MC(speed:Number, startPosition:Point)
{
this.speed = speed;
this.addEventListener(Event.ENTER_FRAME, update);
this.x = startPosition.x;
this.y = startPosition.y;
}
public function update(speed:Number)
{
var screenWidth:Number = Capabilities.screenResolutionX;
var screenHeight:Number = Capabilities.screenResolutionY;
trace("this.y" , this.y);
trace("this is the stage height" , screenHeight);
trace("this.speed" , this.speed);
if (this.y >= screenHeight - 100) { // We're at the left edge
trace("Entered if");
trace("new Starting Pos" , this.y);
this.y = stage.height;
parent.removeChild(this);
this.removeEventListener(Event.ENTER_FRAME, update);
this.dispatchEvent(new Event(Event.COMPLETE));
}
else this.y += this.speed;
}
}
}
In the base FLA file, i create the main object that will collide with all the obstacles created using Mc_MC and Mc_MC2. I create it using the following code :
Home.fla
import flash.events.*
//import flash.events.EventDispatcher.addEventListener()
import flash.display.DisplayObject;
//import flash.events.MouseEvent;
import flashx.textLayout.events.UpdateCompleteEvent;
import flash.display.MovieClip;
var offsetX:Number;
var offsetY:Number;
//var draggedObject:DisplayObject;
var my_obj:OriginalObject = new OriginalObject();
//left.addEventListener(MouseEvent.MOUSE_MOVE, drag);
//The speed of the scroll movement.
var scrollSpeed:uint = 2;
//This adds two instances of the movie clip onto the stage.
var s1:ScrollBg = new ScrollBg();
var s2:ScrollBg = new ScrollBg();
left.addEventListener(MouseEvent.MOUSE_DOWN,mouseDown);
function mouseDown(e:MouseEvent):void {
stage.addEventListener(MouseEvent.MOUSE_UP,mouseUp); //listen for mouse up on the stage, in case the finger/mouse moved off of the button accidentally when they release.
addEventListener(Event.ENTER_FRAME,myButtonClick); //while the mouse is down, run the tick function once every frame as per the project frame rate
}
function mouseUp(e:MouseEvent):void {
removeEventListener(Event.ENTER_FRAME,myButtonClick); //stop running the tick function every frame now that the mouse is up
stage.removeEventListener(MouseEvent.MOUSE_UP,mouseUp); //remove the listener for mouse up
}
right.addEventListener(MouseEvent.MOUSE_DOWN,mouseDown2);
function mouseDown2(e:MouseEvent):void {
stage.addEventListener(MouseEvent.MOUSE_UP,mouseUp2); //listen for mouse up on the stage, in case the finger/mouse moved off of the button accidentally when they release.
addEventListener(Event.ENTER_FRAME,stopDragging); //while the mouse is down, run the tick function once every frame as per the project frame rate
}
function mouseUp2(e:MouseEvent):void {
removeEventListener(Event.ENTER_FRAME,stopDragging); //stop running the tick function every frame now that the mouse is up
stage.removeEventListener(MouseEvent.MOUSE_UP,mouseUp2); //remove the listener for mouse up
}
my_obj.x = stage.width / 2;
my_obj.y = stage.height - (stage.height / 3 );
stage.addChild(my_obj);
function myButtonClick(ev:Event):void
{
trace("UPPP");
if(my_obj.x > (my_obj.width*2)){
my_obj.x = my_obj.x - 10;
trace("In the limit");
}
else {
trace("out of bounds");
}
trace("myButton has been clicked.");
}
//// This function is called when the mouse button is released.
function stopDragging(ev2:Event):void
{
trace("Down");
if(my_obj.x <= right.x){
my_obj.x = my_obj.x + 10;
}
}
How can I test the collision of the moving Mc_MC / Mc_MC2 with my_obj considering that the come from different AS files?
I am new to AS, so any help would be appreciated!
If you want to be able to collision test objects from different classes / parentage, then you need to set up your project in a way that you can gain a reference to said objects.
From the looks of it, your two objects are actually in the same class already (as your main timeline code and document class code share the same scope, so what you declare in one should be available in the other).
The only thing you are missing, is a top-level reference to your obstacle/Mc_MC. As currently you assign it to a var that is scoped to the makeSymbol function (so you only have a reference to it inside that function).
Solution
In your document class (baseCode.as) , create a top-level var to hold a reference to that obstacle: (for reference, you have a top level var called t, put this line above or below that)
private var obstacle:Mc_MC;
later in your function that instantiates the new Mc_MC (makeSymbol), assign the instance to that top level var:
obstacle = new Mc_MC(1 + Math.random() *10, loc);
addChild(obstacle);
Now you can access that obstacle var anywhere else in the main timeline or document class.
if(my_obj.hitTest(obstacle)){
}
As an aside, if you have a document class, there is no point in having code on the first frame of your main timeline as that code would work the same in your document class (though it has to be contained in a function). Here is an example of where to move main timeline code:
public class Main extends MovieClip {
public function Main():void {
//listen for the added to stage event prior to do anything display oriented
this.addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event):void {
//this is the best place to put the equivalent of timeline code (not including vars or functions, put those in the class root NOT nested here)
}
}

ActionScript3: removeEventListener with function parameter

how can i remove EventListener from MovieClip in actionscript? Below is my sample code.
import flash.events.MouseEvent;
import flash.geom.ColorTransform;
var color: ColorTransform = new ColorTransform();
color.color = 0x00CC66;
colorChange_mc.buttonMode = true;
colorChange_mc.addEventListener(MouseEvent.MOUSE_OVER, changeColor(color));
colorChange_mc.addEventListener(MouseEvent.MOUSE_OUT, changeColorToNormal);
function changeColor(color: ColorTransform): Function {
return function (e: MouseEvent): void {
colorChange_mc.transform.colorTransform = color;
}
}
function changeColorToNormal(e: MouseEvent): void {
var color: ColorTransform = new ColorTransform();
color.color = 0x000033;
colorChange_mc.transform.colorTransform = color;
}
changer_mc.buttonMode = true;
changer_mc.addEventListener(MouseEvent.MOUSE_DOWN, removeEvent);
function removeEvent(e: MouseEvent): void {
colorChange_mc.removeEventListener(MouseEvent.MOUSE_OVER, changeColor(color));
}
I created two MovieClips on the stage , colorChange_mc has two EventListeners one for mouseDown and one for mouseOut. When change_mc button is pressed I want to remove one of the EventListener. I was able to remove EventListener without passing parameter to changeColor function. But in my real class this parameter plays crucial role.
Use a class-var instead - one you have already in your code. To keep straight I created a second ColorTransform as a class-var - normalColor
import flash.events.MouseEvent;
import flash.geom.ColorTransform;
var color: ColorTransform = new ColorTransform();
color.color = 0x00CC66;
var normalColor: ColorTransform = new ColorTransform();
normalColor.color = 0x000033;
colorChange_mc.buttonMode = true;
colorChange_mc.addEventListener(MouseEvent.MOUSE_OVER, changeColor);
colorChange_mc.addEventListener(MouseEvent.MOUSE_OUT, changeColorToNormal);
function changeColor(e:MouseEvent): void{
colorChange_mc.transform.colorTransform = color;
}
function changeColorToNormal(e:MouseEvent): void {
colorChange_mc.transform.colorTransform = normalColor;
}
changer_mc.buttonMode = true;
changer_mc.addEventListener(MouseEvent.MOUSE_DOWN, removeEvent);
function removeEvent(e:MouseEvent): void {
colorChange_mc.removeEventListener(MouseEvent.MOUSE_OVER, changeColor);
}
Greetings.
The problem is that in order to remove the listener, you have to identify the function that you originally passed to it. But you have no reference to the function, because it is anonymous and lives in the global scope.
Think object oriented. Put all the functionality of changing the color into a class. All you actually want to do is
specify a color value
be able to disable the whole thing again.
Add two methods to the class of colorChange_mc that let you do this:
public function set color(value:uint):void
and
public function set disable(value:Boolean):void
Alternatively, you can override the enabled setter function to tie your logic to it, if this is something you desire.
This way you can refactor most of the code that currently bloats your main time line into the class.
According to my first answer and your comment with new facts here is a solution for more buttons/colors using a Dictionary...
import flash.events.MouseEvent;
import flash.geom.ColorTransform;
import flash.utils.Dictionary;
import flash.display.MovieClip;
var normalColor: ColorTransform = new ColorTransform();
normalColor.color = 0x000033;
//button1-30 are the instancenames of your buttons
var initObj:Array=[{btn:button1, color:0xFFFFFF},
{btn:button2, color:0xFF0000},
{btn:button3, color:0x00FF00},
...
{btn:button30, color:0x000000},
];
var dict:Dictionary = new Dictionary();
initButtons();
function initButtons():void
{
for each (var item:Object in initObj)
{
var btn = item.btn;
btn.addEventListener(MouseEvent.MOUSE_OVER, changeColor);
btn.addEventListener(MouseEvent.MOUSE_OUT, changeColorToNormal);
dict[btn] = {color:item.color};
}
}
function changeColor(e:MouseEvent):void
{
var btn:MovieClip = e.target as MovieClip;
var color:uint = dict[btn].color;
var cT:ColorTransform=new ColorTransform();
cT.color = color;
colorChange_mc.transform.colorTransform = cT;
}
function changeColorToNormal(e:MouseEvent):void
{
colorChange_mc.transform.colorTransform = normalColor;
}
Greetings.
Thank you for your replies. As I said before the code that i posted above was just an example. Sorry if I asked unclear question. I solved my problem by disabling some specific buttons when colorChange button is pressed.
_userCardsButtons_mc.mouseEnabled = false;
_userCardsButtons_mc.mouseChildren = false;
_userCardsButtons_mc.buttonMode = false;
this code disabled event listeners that I added before.

Flash AS3 Error: 1013: The private attribute may be used only on class property definitions

C:\Users\Lab3project\MainDocument.as, Line 103, Column 7 1013: The
private attribute may be used only on class property definitions.
The lines of code concerned:
package {
//these are flash built-in classes
import flash.display.MovieClip;
import flash.events.TimerEvent;
import flash.utils.Timer;
import flash.events.Event;
//Our own custom class
import MainTimer;
public class MainDocument extends MovieClip{
private var gameTimer:MainTimer;
private var thePlayer:Player;
private var theEnemy:Enemy;
private var maxEnemies: int = 3;
private var e:int = 0;
private var childrenOnStage:int;
public function MainDocument() {
// constructor code
trace("the main document is alive");
// new instance MainTimer class
gameTimer = new MainTimer();
// must add it to the stage
addChild(gameTimer);
//adjust its postion on stage
gameTimer.x = 20;
gameTimer.y = 20;
//add the player
thePlayer = new Player();
addChild(thePlayer);
// adjust its postion on the stage
thePlayer.x = stage.stageWidth * 0.5;
//assign the name property
thePlayer.name = "player";
while(e < maxEnemies){
createEnemy();
e++;
} //end while
//Update this variable every time a child is added to the stage
childrenOnStage = this.numChildren
// Add event listener to control timing of main game loop
addEventListener(Event.ENTER_FRAME,mainGameLoop);
}//end function MainDocument
private function createEnemy():void{
trace("create enemy");
theEnemy = new Enemy();
addChild(theEnemy);
//Place in a random spot on stage
theEnemy.x = (Match.random() * stage.stageWidth);
theEnemy.y = 0;
//assign the name property
theEnemy.name = "enemy";
//Update this variable every time a child is added to the stage
childrenOnStage = this.numChildren
} //end function createEnemy
//the main loop for the game
private function mainGameLoop(event:Event): void{
checkForGameReset();
processCollisions();
scrollStage();
} // end functiom mainGameLoop
private function checkForGameReset():void{
//define conditions
} //end function checkForGameReset
private function processCollisions():void{
//set up the main loop to look through all collidale objects on stage
for(var c:int;c < childOnStage;c++){
//trace ("Child on stage c= " + c +
//test for a player of enemy child on stage
if (getChildAt(c).name == "player" || getChildAt(c).name == "enemy"){
//see if object is touching the game stage
if( theGameStage.hitTestPoint(getChildAt(c).x, getChildAt(c).y,true)){
//while it is still touching the game stage
while( theGameStage.hitTestPoint(getChildAt(c).x, getChildAt(c).y,true)==true){
//called from CollisionObject class,so force the connection
CollisionObject(getChildAt(c)).incrementUpward();
if( theGameStage.hitTestPoint(getChildAt(c).x, getChildAt(c).y,true)==false){
}CollisionObject(getChildAt(c)).keepOnBoundary(); //make it stick
} // end if
} // end while
} //end if touching
} //end if player or enemy
} //end for loop
} //end function processCollisions
The lines of code concerned: is here where im getting the error
private function scrollStage(): void
{
// figure out logic
}
// end function scrollStage
//add the enemy for testing
theEnemy = new Enemy();
addChild(theEnemy);
// adjust its postion on the stage
theEnemy.x = stage.stageWidth * 0.5;
theEnemy.y = 200;
} // end public function MainDocument
} // end public class
} // end package
After I have change that code ,it then says I have a extra character at the end but the characters I have to have at the end.
Problems like this often occur because of a slight typo in syntax. Perhaps you have accidentally declared your private function within another function?
public function doSomething() {
//a whole bunch of stuff
//then we forget to close the function with a brace "}"
private function scrollStage() {
//but we're still defining doSomething! Throw error!
}
Go and look for errors like this, and hopefully you can find the problem.

as3 - dispatch mouse event from external class

I am having problems understanding correctly how to dispatch events and capture them in another class.
In this case, I am trying to emulate a mouse click dispatched from an "clickM" class.
On stage I have 2 movieclips to test, a custom cursor and the listeners to capture the click event.
clickM:
package {
import flash.display.MovieClip;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.events.Event; //dispatcher
import flash.events.MouseEvent;// mouse event
public class clickM extends MovieClip {
private var delay: uint = 3000;
private var repeat: uint = 0; //se va por todo el tiempo
private var myTimer: Timer = new Timer(delay, repeat);
public function clickM() {
myTimer.start();
myTimer.addEventListener(TimerEvent.TIMER, timerHandler);
}
private function timerHandler(e: TimerEvent): void {
//repeat--;
//statusTextField.text = ((delay * repeat) / 1000) + " seconds left.";
trace ( "simulate click...");
//dispatchEvent(new MouseEvent(MouseEvent.CLICK));
this.dispatchEvent(new MouseEvent(MouseEvent.CLICK, true, false));
}
}
}
Stage code, rojo & morado are movieclips:
import flash.events.MouseEvent;
stage.addEventListener(Event.ENTER_FRAME, myFunction);
var mano: clickM = new clickM();
mano.name = "mano";
addChild (mano);
morado.addEventListener(MouseEvent.CLICK, cl);
rojo.addEventListener(MouseEvent.CLICK, cl);
stage.addEventListener(MouseEvent.CLICK, cl);
function myFunction(event: Event) {
mano.x = mouseX;
mano.y = mouseY;
}
function cl(e: MouseEvent) {
trace("click over " + e.target.name);
}
If I click over morado or rojo, there's no problem - I can get their names. If I just let the code run, I can't get their names, I just get "mano", which is the custom cursor I'm using.
How can I get the desired behavior?
Regards.
Add mouseEnabled=false; inside clickM constructor. This should make Flash to ignore your mano in the event dispatch phase, so the underlying object should be the primary target if there's any, otherwise the target will be the stage. If your custom cursor contains more movie clips, you should also add mouseChildren=false;.
public function clickM() {
mouseEnabled=false;
// possibly add this too
mouseChildren=false;
myTimer.start();
myTimer.addEventListener(TimerEvent.TIMER, timerHandler);
}

Add an array of Movie Clips to a filter ActionScript 3

i stored 8 mc's in an array an i put them on the stage.
now I want to apply to these mc's a blur effect.
My problem is that i don't know how to apply for every mc the blur effect by clicking on it.
So for example I have all the mc's on the stage and if I click on one of them the clicked one should have the blur effect and so on.
How to apply the filter to the mc's?
Thank you for you time
You can loop through the mc's array and add event listeners on each of them:
var mcArrayLength:int = mcArray.length();
for (var i:int = 0; i < mcArrayLength; i++) {
var mc:MovieClip = mcArray[i] as MovieClip;
mc.addEventListener(MouseEvent.CLICK, onMcClick);
}
and the event handler:
function onMcClick(e:MouseEvent):void
{
var clickedMc:MovieClip = e.currentTarget as MovieClip;
clickedMc.filters = [myBlurFilter];
}
of course, if you wanted to have only one blurred mc at a time, you should keep a reference to it and remove blur once another mc is clicked:
var currentlyClickedMc:MovieClip;
function onMcClick(e:MouseEvent):void
{
var clickedMc:MovieClip = e.currentTarget as MovieClip;
clickedMc.filters = [myBlurFilter];
if (currentlyClickedMc) currentlyClickedMc.filters = [];
currentlyClickedMc = clickedMc;
}
something like this (untested) should work:
package
{
//Imports
import flash.events.MouseEvent;
import flash.filters.BlurFilter;
//Class
public function ClickToBlur
{
//Variables
private var clickableObjects:Array;
//Constructor
public function ClickToBlur(clickableObjects:Array)
{
this.clickableObjects = clickableObjects;
init();
}
//Initialize
private function init():void
{
for each (var object:Object in clickableObjects)
{
object.addEventListener(MouseEvent.CLICK, clickMouseEventHandler);
}
}
//Click Mouse Event Handler
private function clickMouseEventHandler(evt:MouseEvent):void
{
if (evt.currentTarget.filters == [])
{
evt.currentTarget.filters = [new BlurFilter()];
}
else
{
evt.currentTarget.filters = [];
}
}
//Dispose
public function dispose():void
{
for each (var object:Object in clickableObjects)
{
object.removeEventListener(MouseEvent.CLICK, clickMouseEventHandler);
}
}
}
}
just pass your array to a new instance of the class
var ctb:Object = new ClickToBlur(myArrayOfObjects);
and clean up when your done:
ctb.dispose();
since they're all Movie Clips, you should use a Vector instead of an Array.