AS3: int used where a boolean value was expected - actionscript-3

I have three different buttons. When you click one of the buttons, it is supposed to activate the stageSelect function, which should then output the button's number.
But when I do that, I get the error in the title. What am I doing wrong here?
package {
import flash.display.MovieClip;
import flash.display.SimpleButton;
import flash.events.MouseEvent;
import flash.ui.Mouse;
public class MenuScreen extends MovieClip {
public function MenuScreen() {
Mouse.show();
selectGrass.addEventListener(MouseEvent.CLICK, stageSelect, 1);
selectDirt.addEventListener(MouseEvent.CLICK, stageSelect, 2);
selectGravel.addEventListener(MouseEvent.CLICK, stageSelect, 3);
}
public function stageSelect(stageID:Number) {
trace(stageID);
}
}
}

this is because the third param for the method addEventListener is useCapture which requires a boolean saying that you wish to grab the event during the capture phase before bubbling. You are calling
selectGrass.addEventListener( MouseEvent.CLICK, StageSelect, 1);
What you need to do instead is
selectGrass.addEventListener( MouseEvent.CLICK, grassSelected);
selectDirt.addEventListener( MouseEvent.CLICK, dirtSelected);
private function grassSelected(event:MouseEvent):void{
// do grass stuff
}
private function dirtSelected(event:MouseEvent):void{
// do dirt stuff
}

Related

Main class was not the first class which was called in ActionScript 3.0

I have a weird problem in a game which I want to create. At first I have created a project without external classes.
On the root I have three Characters and one Level. Also there is a script for the key listeners and I have eventListeners to register the level, levelElements, coins and the characters. Then I have a CharacterControl MovieClip in the library. This MovieClip contains the character behaviour. As example walk, jump, idle, gravity if not colliding to the ground. There are also different events and eventListeners.
The scripts are on the timeline. If I call in both timelines a trace-function, the root was called before the CharacterController.
After that in my next exercise I created a document class Main. Now there are all root scripts. And for the CharacterController I also copied the timeline code and put it into an external class.
Now my problem is that the CharacterController class is called before the main class gets called. This leads to the problem that the eventListener and events can't get called in right order. There are happening a few errors. No Coin and no Character collides on the ground or a plattform. Everything is falling down.
How can I achieve that the Main gets called at first? Should I remove the characters and create them by script?
EDIT:
Ok, I give a short example which shows the basic problem without the complex code of my game.
package {
import flash.display.MovieClip;
public class Main extends MovieClip {
public function Main() {
trace("main was called");
}
}
}
package {
import flash.display.MovieClip;
public class My_Circle extends MovieClip {
public function My_Circle() {
// constructor code
trace("circle was called");
}
}
}
Here are some pictures of the configuration and structure of my project:
I need Main called as first. I think it's a basic problem in as3.
You'd make the class file of your stage Main.as in the properties pane.
Edit: Interesting. Just replicated this. I believe then that flash/air constructs elements so they're ready to be put on the stage instead of constructing the stage first and elements after. You should put the code you want to execute for your circle in some sort of init function and execute it in.
package
{
import flash.display.MovieClip;
public class Main extends MovieClip
{
public function Main()
{
super();
trace("Hello");
(circle_mc as Circle).init();
}
}
}
Circle:
package
{
import flash.display.MovieClip;
public class Circle extends MovieClip
{
public function Circle()
{
super();
}
public function init():void
{
trace("World");
}
}
}
ok, I figured out a simple solution how you can make it by code. At first I think it's a better solution to create the object (circle, character, whatever) by code.
The timeline code:
import flash.events.Event;
Main.setStageRef(this.stage); //Super-important
stop();
The Main class code:
package {
import flash.display.MovieClip;
import flash.display.Stage;
import flash.display.DisplayObject;
import flash.events.Event;
public class Main extends MovieClip {
public static var stageRef : Object = null;
var movieClipExample : My_Circle;
public function Main() {
this.addEventListener(Event.ENTER_FRAME,this.afterMainTimeLine);
stage.addEventListener("myEvent", myEventFunction);
}
public function afterMainTimeLine(e:Event) {
trace("Hello");
movieClipExample = new My_Circle;
movieClipExample.init();
stageRef.addChild(movieClipExample);
removeEventListener(Event.ENTER_FRAME, this.afterMainTimeLine);
this.addEventListener(Event.ENTER_FRAME,this.updateFunction);
}
public function updateFunction(e:Event){
movieClipExample.moveFunction();
}
public function myEventFunction(_event: Event) {
trace("myEvent was triggered");
}
//Getter/setter for stage
public static function setStageRef(_stage : Object) : void
{
stageRef = _stage;
}
public static function getStageRef() : Object
{
return stageRef;
}
}
}
The Object code (as example My_Circle):
package {
import flash.display.MovieClip;
import flash.display.Stage;
import flash.display.DisplayObject;
public class My_Circle extends MovieClip {
public function My_Circle()
{
stageRef = Main.getStageRef();
trace("World");
this.x = x;
this.y = y;
var evt = new Event("myEvent", true);
stageRef.dispatchEvent(evt);
}
public function init():void
{
trace("Created Circle");
this.x = 300;
this.y = 100;
}
function moveFunction(){
this.y += 1;
}
}
}
The output is following:
Hello
World
myEvent was triggered
Created Circle
Maybe this could be helpful for others.
Just one thing. For different objects from the same class it's better to use an array. The position (maybe) should be random.

How do I create rollover events in actionscript 3.0 in the DOCUMENT CLASS, not the timeline

I have an animation that I want to function as button and I want it to play the animation when there is a rollover event. I have no clue how to do this.
So far I have this:
package
{
import flash.display.MovieClip;
import flash.events.MouseEvent;
public class optbtn
{
public function optbtn()
{
//
}
}
}
in your constructor would be where I would start, although you could add the listeners in a another function called from your constructor instead.
package
{
import flash.display.MovieClip;
import flash.events.MouseEvent;
public class optbtn
{
public function optbtn()
{
addEventListener( MouseEvent.ROLL_OVER, onRolloverResponse )
}
private function onRolloverResponse( e:MouseEvent ):void
{
// do something here when rolled over
// e.target is the object that was rolled over
// so if you wanted it to play, you might call
// e.target.gotoAndPlay( 2 );
}
}
}
Class names are typically capitaized, ie Optbtn rather than your optbtn

AS3, Adding an event listener to a constructor class?

this is probably really simple I cannot for the life of me work out why this is not working. I am trying to create an object (only for testing) and assign event listeners within the constructor. In my head it should work but I am sure I must be missing something:
package {
import flash.display.MovieClip;
import flash.events.MouseEvent;
public class Box extends MovieClip {
public function Box() {
// constructor code
var mySound:Sound = new Bark();
trace("Box created");
height=800;
width=300;
x=100;
y=100;
addEventListener(MouseEvent.MOUSE_OVER, overThis);
addEventListener(MouseEvent.CLICK, clickToPlay);
}
public function overThis(m:MouseEvent){
trace("SADF");
}
function clickToPlay(m:MouseEvent){
mySound.play();
}
}}
By doing this i wanted the "box" to be self sufficient in regards to managing its own events. (Please ignore stuff like play(), that all works when I run within the MAINDOC.as directly.
This is the main doc:
package {
import flash.display.MovieClip;
import flash.media.Sound;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.ColorTransform;
public class MainDoc extends MovieClip
{
public function MainDoc()
{
// constructor code
init();
function init()
{
createBox(300,300);
}
function createBox(newX,newY)
{
var box = new Box();
box.x = newX;
box.y = newY;
addChild(box);
}
}
}}
When I test it creates the box (which i have drawn) but does not run any events?
Hope you guys can help
Ray
You have to add some graphics to your box so your MouseEvents can work:
public function Box()
{
// constructor code
var mySound:Sound = new Bark();
trace("Box created");
// begin the filling of some graphics, you can change color/alpha as you want
this.graphics.beginFill( 0x000000, 1 );
// make a rectangle 300x800
this.graphics.drawRect(0,0,300,800);
// stop filling
this.graphics.endFill();
// you don't need it anymore
//height=800;
// you don't need it anymore
//width=300;
// place your clip where you want but you do that in the Main class so no need there
//x=100;
//y=100;
// now you have graphics attached to your MovieClip the MouseEvent must work
addEventListener(MouseEvent.MOUSE_OVER, overThis);
addEventListener(MouseEvent.CLICK, clickToPlay);
}
Hope that will help you :)

Can I create EventListener in AS3 from one Object and remove it from another Object?

I spent lots of time trying to resolve this issue. So, basically I have an class (addadd) that contain function which can create eventListener and remove eventListener. And I have another two objects (Symbol1, Symbol2) that tell the function what to do - create or remove.
package {
import flash.display.*;
import flash.events.Event;
import fl.motion.MotionEvent;
import flash.events.MouseEvent;
public class addadd
{
var stanishev:stanishev_line = new stanishev_line;
public function addadd()
{
// constructor code
}
public function stanishevF(par1)
{
if (par1 == "create")
{
Main.display.addChild(stanishev);
stanishev.name = "stanishev_child";
stanishev.x = -200;
stanishev.y = 500;
stanishev.gotoAndPlay("start");
stanishev.addEventListener(Event.ENTER_FRAME, frameDOstanishev);
}
else
{
trace ("asasasas");
stanishev.removeEventListener(Event.ENTER_FRAME, frameDOstanishev);
}
}
public function frameDOstanishev(e:Event)
{
trace (stanishev.currentFrame);
}
} }
package {
import flash.display.SimpleButton;
import flash.events.MouseEvent;
public class Symbol1 extends SimpleButton
{
var call_creator:addadd = new addadd;
public function Symbol1()
{
// constructor code
addEventListener(MouseEvent.MOUSE_OVER, eventResponse);
addEventListener(MouseEvent.MOUSE_DOWN, eventResponse2);
}
function eventResponse(e:MouseEvent)
{
call_creator.stanishevF("create");
}
function eventResponse2(e:MouseEvent)
{
call_creator.stanishevF("destroy");
}
} }
package {
import flash.display.SimpleButton;
import flash.events.MouseEvent;
public class Symbol2 extends SimpleButton
{
var call_creator:addadd = new addadd;
public function Symbol2()
{
// constructor code
addEventListener(MouseEvent.MOUSE_DOWN, eventResponse2);
}
function eventResponse2(e:MouseEvent)
{
call_creator.stanishevF("destroy");
}
} }
So I can make addadd class to create and remove this eventListener from Symbol1 but I am not able to make the addadd class to create this eventListener sending "create" parameter from Symbol1 and remove it from Symbol2 by sending "destroy" parameter!!!
How can I create and remove the same eventListener from different objects? I found this approach creating and killing the listeners for more organized but I am not sure if this is the right way. I am having problems with the listeners (error: 1009) when I move between frames in the main timeline, so I want to kill them all before jump to another frame.
Thanks
You can put public function say, removeListeners() and addListeners() in that class and call those function from other class, like so,
class A ()
{
//constructor code
public function removeListeners():void
{
//remove listeners here
}
}
Then call this removeListeners() public function from the class(e.g. Main.as) where you have created instance of this "A" class.

ActionScript 3 Event doesn't work

I'm new in AS, and trying to make my first application. I have a class, that showld manage some events, like keyboard key pressing:
public class GameObjectController extends Sprite
{
var textField:TextField;
public function GameObjectController()
{
addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
}
private function onKeyDown(event: KeyboardEvent):void
{
trace("123");
}
}
but when I'm running it, and pressing any button, nothing happands. What am I doing wrong?
Try stage.addEventListener() for KeyboardEvents.
The reason for this is that whatever you add the event to needs focus, and the stage always has focus.
If GameObjectController isn't the document class, it will need to have stage parsed to its constructor for this to work, or it will need to be added to the stage first. The former will be less messy to implement.
public function GameObjectController(stage:Stage)
{
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
}
Add the keyboard event to the stage and import the KeyboardEvent class.
package
{
import flash.display.Sprite;
import flash.events.KeyboardEvent;
public class GameObjectController extends Sprite
{
public function GameObjectController()
{
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownEventHandler);
}
private function keyDownEventHandler(event:KeyboardEvent):void
{
trace(event);
}
}
}