I have loaded movieClip to stage and was performing some events on that movieClip. Movie clip has own public functions and variables, and those are NOT accessible through currentTarget object in events.
Here is sample class:
package {
import flash.display.MovieClip;
import flash.display.Shape;
public class SampleClass extends MovieClip {
var str:String;
public function SampleClass() {
str="Some string";
/* draw just a sample rectangle to click on it */
var rectangle:Shape=new Shape ;
rectangle.graphics.beginFill(0x000000);
rectangle.graphics.drawRect(0,0,100,100);
rectangle.graphics.endFill();
}
public function getStr():String {
return str;
}
}
}
And here is loading on the stage and creating event:
package {
import flash.display.MovieClip;
import flash.events.MouseEvent;
public class MainClass extends MovieClip {
var a:SampleClass;
public function MainClass() {
a=new SampleClass();
addChild(a);
a.addEventListener(MouseEvent.CLICK,clickEvent);
}
function clickEvent(evt:MouseEvent):void {
var Obj=evt.currentTarget;
trace (Obj.getStr());
}
}
}
Tracing will return null instead of string value cause currentTarget is an Object, not a Class (movieClip). Any idea how to solve this?
//use this code it will work
function clickEvent(evt:MouseEvent):void {
var Obj:SampleClass = evt.currentTarget as SampleClass;
trace (Obj.getStr());
}
I dont know if your problem is solved now but the code you posted in your question worked okay for me..
What I did to test it..
In a new blank document, open Library (ctrl+L) and right-clicked to make symbol (MovieClip)
In linkage section, tick Export for Actionscript and call it SampleClass
Right click symbol item now added in Library list and choose Edit Class option
In that SampleClass I replace all with a paste of your code BUT NOTE: after that line rectangle.graphics.endFill();.. I also added the line addChild(rectangle);
Now when I test (Debug: Ctrl+Shift+Enter).. I see a black square that traces "Some string" everytime I click it..
Your MainClass.as was attached to the FLA as the Document Class (see Properties with Ctrl+F3)
I hope this is useful to you or anyone else trying this kind of code. Any issues just add a comment. Thanks.
Related
I have this code in AS3:
package clase
{
import flash.display.MovieClip;
import flash.events.Event;
/**
* ...
* #author cry
*/
public class CercNegru extends MovieClip
{
var growthRate:Number = 2;
cerc.addEventListener(Event.ENTER_FRAME,grow);
public function CercNegru()
{
}
private function grow(e:Event):void
{
trace("asdda");
}
}
}
When you run this program receive error:
Line 12 1120: Access of undefined property cerc.
Line 12 1120: Access of undefined property grow.
I put an image to understand better :
Can you help me to solve this problem please?
Thanks in advance!
The errors are because in class files, all functional code needs to live inside a function.
So take this line, which is just floating in the class:
cerc.addEventListener(Event.ENTER_FRAME,grow);
And move into the constructor (assuming you want it to run right away when you instantiate the class):
public function CercNegru()
{
cerc.addEventListener(Event.ENTER_FRAME,grow);
}
In class files, the constructor (which is the function whose name exactly matches the class name), is what get's called you use the new keyword.
So doing new CercNegru() will call that function.
NOW, I'm also assuming that this class file is attached to FlashPro library object, and you have something on the timeline with an instance name of cerc. (if that is not the case, then that is the reason for your error)
Timeline stuff though, isn't always available in the constructor, so you may need to wait until the instance has been added to the screen.
public var cerc:MovieClip; //you may want to create a reference to the timeline item, so you get compile time checking
public function CercNegru()
{
this.addEventListener(Event.ADDED_TO_STAGE, addedToStage);
}
private function addedToStage(e:Event):void {
this.removeEventListener(Event.ADDED_TO_STAGE, addedToStage);
//this is the equivalent of timeline code now
cerc.addEventListener(Event.ENTER_FRAME,grow);
}
package clase
{
import flash.display.MovieClip;
import flash.events.Event;
/**
* ...
* #author cry
*/
public class CercNegru extends MovieClip
{
var growthRate:Number = 2;
var cerc:DisplayObject; // ADD THIS
public function CercNegru()
{
cerc.addEventListener(Event.ENTER_FRAME,grow);
}
private function grow(e:Event):void
{
trace("asdda");
}
}
}
As the error says cerc is undefined. So you should define it. Assuming that your 'cerc' is a Sprite,
var cerc:Sprite;
I am a beginner of action script. working with button in action script class file not working.
i have created two file one is stream.as and another is main.as
main.as is the main class file of my frame.
i have drawed a button and converted it in button and gave instance name play_btn.
but the compiler giving me 1120: access of undefined property play_btn.
the both codes are given below;
main.as
package {
import flash.display.MovieClip;
import stream.stream;
public class main extends stream {
public function main() {
}
// constructor code
}
}
stream.as
package {
import flash.events.MouseEvent;
import flash.display.MovieClip;
public class stream extends MovieClip {
public function main() {
play_btn.addEventListener(MouseEvent.CLICK, pausevedio);
function pausevedio(event:MouseEvent):void{
play_btn.visible=false;
}
// constructor code
}
}
}
Correct me if I'm wrong but it's because the play_btn belongs solely to your main class and you're trying to access it through the stream class, to do this properly try to instantiate it through code in the class you wish to use it in instead of on the timeline like so:
playBtn: play_btn = new play_btn();
playBtn.x = x;
playBtn.y = y;
addChild(playBtn);
This is my best guess I'm not exactly sure how your classes are structured but this might be your issue. I hope this helps (I'm new too but I figured my two-cents might help!)!
~Cheers!
Can anyone help me how to save changes of an object from an fla file into a .as file? Say I have an integer which will increment by one if a button is clicked and let's say I clicked it 5 times which would make it's value from zero to five...How can I send this data into my .as file? I already looked on the internet but I got no clear answers...I'm using actionscript 3.
If you want to 'send' data to an .as file give it a property and, from your Main file, set the property of an instance of that Class, eg:
package {
import flash.display.MovieClip;
// other imports as necessary
public class Tracker extends MovieClip {
private var _numIncrements:int = 0;
public function Tracker() {
}
public function set numIncrements(p_value:int):void {
_numIncrements = p_value
}
}
}
Then, in the variable list of your Main class:
private var _tracker:Tracker = new Tracker();
And in the Main CLICK handler:
_tracker.numIncrements ++;
You should be handling the click and count functionality from your .as file.
Don't start putting code inside your Library Objects. Its a very bad practice
something like this:
package {
//imports
public class Main extends Sprite
{
private var clickCount:int = 0;
public function Main()
{
yourButton.addEventListener(MouseEvent.CLICK, clickHandler);
}
function clickHandler(event_object:MouseEvent) {
clickCount++; // increments by 1
}
}
}
Following code isn't addchilding a lv1 to the stage for some reason, this is the stage class and is attached to the stage, both classes are correct (triple checked...) and no errors are generated...
package
{
import flash.events.*;
import flash.display.*;
public class TankDrive extends MovieClip
{
public var lev1:lv1;
public function TankDrive()
{
lev1 = new lv1();
lev1.x = 0;
lev1.y = 0;
addChild(lev1);
}
}
}
Also I checked against other code that worked and found no differences other the specific variable names which I quadruple-checked...
In your imports try adding the lv1.as file and see if it helps kill that null error.
import flash.events.*;
import flash.display.*;
import lv1; //imports the lv1.as file
public class TankDrive extends MovieClip
{
public var lev1:lv1;
public function TankDrive()
{
lev1 = new lv1();
lev1.x = 0;
lev1.y = 0;
addChild(lev1);
}
}
EDIT TWO ------------ After re-reading comments -----------
null line in other class is
stage.addEventListener(KeyboardEvent.KEY_DOWN, keypush);... I do have
public function keypush(event:KeyboardEvent):void { } in it...
Get rid of stage. and only just have addEventListener(KeyboardEvent.KEY_DOWN, keypush);
The line addChild(lev1); in TankDrive already gives contents of lv1 some access to stage so in lv1 just writing addChild is enough.
NOTE: This is true for most eventListeners (mouse/timers etc) and also display objects.
When you need to explicitly access the stage (especially for stage+keyboard listeners) you must setup your lv1.as like so:
Add a listener to check for when the stage is available to lv1 contents.
If available then get entire stage to listen for keyboard controls.
public function lv1() {
addEventListener(Event.ADDED_TO_STAGE, stageAvailable);
//Your other code here...
}
private function stageAvailable(e:Event):void {
trace("(LV1.AS): ADDED_TO_STAGE was successful");
removeEventListener(Event.ADDED_TO_STAGE, stageAvailable);
stage.addEventListener(KeyboardEvent.KEY_DOWN, keypush);
//Your other keyboard listeners code...
}
You must inherit lev1 class from DisplayObject or any class inherited from DisplayObject (eg. Sprite, MovieClip etc). addChild method accepts only DisplayObject instances.
I am currently having problems referencing a MovieClip child which I add to the Stage from the Document Class. Basically when the MovieClip child is added to the Stage from the Document Class, I want a certain MovieClip already on the Stage to reference it once it is on the Stage.
Also, if it is possible, I don't want the MovieClip referencing the child being added to the Stage to have parameters linking it with the Document Class, because I plan on nesting this MovieClip within another MovieClip later on in the future.
Here is the code for the MovieClip class which is referencing the child once it is added to the Stage:
package com.gameEngine.assetHolders
{
import com.gameEngine.documentClass.*;
import com.gameEngine.assetHolders.*;
import com.gameEngine.assetHolders.Levels.*;
import flash.display.*;
import flash.events.*;
public class FallingPlatform extends MovieClip
{
public var _document:Document;
// Trying to reference "_player"
public var _player:Player;
public var fallState:Boolean;
public var platformYSpeed:Number = 0;
public var platformGravityPower:Number = 0.75;
public function FallingPlatform()
{
this.addEventListener(Event.ADDED_TO_STAGE, initFallingPlatform);
// constructor code
}
public function initFallingPlatform(event:Event)
{
this.addEventListener(Event.ENTER_FRAME, dynamicFall);
this.addEventListener(Event.ENTER_FRAME, hitTest);
}
public function dynamicFall(event:Event)
{
if (this.fallState)
{
this.platformYSpeed += this.platformGravityPower;
y += this.platformYSpeed;
}
}
// Trying to reference "_player"
public function hitTest(event:Event)
{
if (this.hitTestPoint(_player.x, _player.y + 1, true))
{
this.fallState = true;
}
}
}
}
The player is initialized in the Document class, right? So for me, the best option is either passing the player reference in the constructor of your FallingPlatform class like this
public function FallingPlatform (thePlayer:Player) {
this._player = thePlayer
}
or having a setter method to pass it to it. In this way, you're not tying the structure of your code
public function set player (thePlayer:Player):void {
this._player = thePlayer
}
Hope it helps!
If you set a document class for a fla file every movieclip on the stage can be accessed by it's instance name - just as you wold create a var with its name.
Event more, you can do something like that:
If you place two movieclips on the stagefor example mc1 and mc2 you can add them as variables to the document class.
package{
public class DocClass{
public var mc1:MovieClip;
public var mc2:MovieClip;
[...]
}
}
and than you can access those movieclips from your class with code hints form your IDE (flash or flashbuilder)
the opposite is also availible: define variables in your class and than access them in flash
! it works best when your document class extends a Sprite, I haven;t tested it on extending from a MovieClip but it should also work