AS3 Access of undefined property event - actionscript-3

I'm learning AS3 maybe this a simple question but I can't find what is wrong...
I receive this error
Scene 1, Layer 'AS3', Frame 1, Line 1, Column 1 1120: Access of undefined property event.
this is my code and my MC instance name is Rueda_mc
import flash.events.Event;
import flash.display.MovieClip;
var Rueda_mc:MovieClip
addEventListener(Event.ENTER_FRAME, rotar);
function rotar(evento: Event): void {
Rueda_mc.rotation += 10;
}

Try this:
import flash.events.Event;
import flash.display.MovieClip;
var Rueda_mc:MovieClip;
addEventListener(Event.ENTER_FRAME, rotar);
function rotar(event:Event): void {
Rueda_mc.rotation += 10;
}

Related

variable in class not returning updated value

I'm relatively new to actionscript 3, and this one has me stumped. Here's my class;
package {
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.events.Event;
public class Clicker extends MovieClip {
public var clicks:uint;
public var string:String;
public function Clicker() {
// constructor code
addEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
addEventListener(Event.ADDED_TO_STAGE, alignCentre);
string = "clicks: ";
clicks = 5; // I can change it here
}
public function clicked(e:MouseEvent):void{
clicks++;
trace(clicks); // it outputs updated value here
}
public function alignCentre(e:Event):void{
x = stage.stageWidth / 2 - width/2;
y = stage.stageHeight / 2 - height/2;
}
public function addedToStageHandler(e:Event):void{
this.stage.addEventListener(MouseEvent.CLICK, clicked);
}
public function get_clicks():uint{
trace(clicks); // gives me whatever I initialise it to in the constructor
return clicks;
}
}
}
I want to return the value of clicks from my Clicker class, but the value remains whatever I set it to in the constructor within get_clicks(), and I'm not sure why.
The variable has class scope, so why would it return the default value (in this case, 5) of clicks from get_clicks()? my clicked() method traces the correctly updated value. Is it a scope issue? I'm very confused.
This is my first frame where I create the object;
import flash.display.DisplayObject;
import flash.events.MouseEvent;
import flash.text.TextField;
var clicks:TextField = new TextField();
var circle:Clicker = new Clicker();
clicks.text = circle.get_clicks().toString();
trace(circle.get_clicks());
addChild(circle);
addChild(clicks);
As you'd expect from the problem I'm having, trace spits out 5 over and over, and Clicks doesn't change from 5.
Edit:
There was a mistake, but fixing it has caused clicks not to update at all. I had a library version of the movieclip on my first frame rather than adding the object to the frame with addChild. Now that I've added circle, clicks does not update as my clicked() method isn't being triggered when I click my object.
Resolved:
import flash.display.DisplayObject;
import flash.events.MouseEvent;
import flash.text.TextField;
var clicks:TextField = new TextField();
var circle:Clicker = new Clicker();
var myTimer:Timer = new Timer(100,0);
myTimer.addEventListener(TimerEvent.TIMER, timerListener);
function timerListener (e:TimerEvent):void{
trace("Timer is Triggered");
trace(circle.get_clicks());
clicks.text = circle.get_clicks().toString();
addChild(circle);
addChild(clicks);
}
myTimer.start();
Rather than use the timeline and frames, I wrapped the code I wanted to repeat in a timer listener. Works exactly as intended.

AS3 Errors 1180 and 1046

I am new to AS3 and still trying to understand it. I am trying to import a class from a different .as file in my main .fla file. I know that the errors are the result of me trying to reference a class that the .fla file doesn't have.
Scene 1, Layer 'Code', Frame 1, Line 6 1180: Call to a possibly undefined method bg.
Scene 1, Layer 'Code', Frame 1, Line 6 1046: Type was not found or was not a compile-time constant: bg.
Scene 1, Layer 'Code', Frame 1, Line 4 1172: Definition thebackground:bg could not be found.
Scene 1, Layer 'Code', Frame 1, Line 4 1172: Definition thebackground:bg could not be found.
it probably has something to do with me importing incorrectly, but what that mistake may be I can't say I have any idea. I added a constructor to instantiate bg (at least I think I did.) Will continue to scout for more info, but everything I've found hasn't been anything that I can gather a working result from.
with that said, here is what I have thus far:
import flash.events.EventDispatcher;
import flash.events.Event;
import flash.events.KeyboardEvent;
import thebackground.bg;
var testbg:bg = new bg;
var leftPressed:Boolean = false;
var rightPressed:Boolean = false;
var upPressed:Boolean = false;
var downPressed:Boolean = false;
testbg.moveBackGround();
This is what I have in my main .fla file at present.
package thebackground
{
import flash.events.EventDispatcher;
import flash.events.Event;
import flash.display.MovieClip;
public class bg extends MovieClip
{
var xScrollSpeed:int = 10;
var yScrollSpeed:int = 10;
public function moveBackGround(event:Event):void
{
if(leftPressed)
{
BG.x += xScrollSpeed;
}
else if(rightPressed)
{
BG.x -= xScrollSpeed;
}
else if(upPressed)
{
BG.y += yScrollSpeed;
}
else if(downPressed)
{
BG.y -= yScrollSpeed;
}
}
}
}
and this is what is in thebackground.as

Access of undefined property errors

I'm getting quite a few errors after I started learning AS3 (Trying to dynamically add the crosshair to the stage instead of using mouse cursor for shooter game):
Main.as, Line 13 1180: Call to a possibly undefined method addChild.
Main.as, Line 13 1120: Access of undefined property crosshair.
Main.as, Line 20 1120: Access of undefined property stage.
Main.as, Line 20 1120: Access of undefined property moveCursor.
You have no constructor in your class.
try this :
package {
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.ui.Mouse;
import flash.media.Sound;
import flash.media.SoundChannel;
public class Main extends MovieClip {
public var crosshair:crosshair_mc;
public function Main()
{
//This creates a new instance of the cursor movie clip and adds it onto
//the stage using the addChild method.
crosshair = new crosshair_mc();
addChild(crosshair);
//Hides the default cursor on the stage so it will not be shown.
Mouse.hide();
//Adds an event listener onto the stage with the enter frame event which
//repeatedly executes the moveCursor function.
stage.addEventListener(Event.ENTER_FRAME, moveCursor);
}
//This function set the x & y positions of the custom cursor to the x & y positions
//of the default cursor.
function moveCursor(event:Event)
{
crosshair.x=mouseX;
crosshair.y=mouseY;
}
}
}

Error 1119 when creating "Load Text" button

A section of a Flash animation I'm creating involves an area where people can write on a notepad, save their work and update it at a later time. The file will be downloaded by users before they run it, rather than from a webpage. Here is the code I have so far:
import flash.events.MouseEvent;
import flash.net.FileReference;
import flash.display.MovieClip;
import flash.events.Event;
stop();
var MyNotes:FileReference = new FileReference()
Save_btn.addEventListener (MouseEvent.CLICK, SaveText);
function SaveText(Event:MouseEvent):void {
MyNotes.save(TypeOwn_txt.text, "MyNotes.txt");
}
Load_btn.addEventListener (MouseEvent.CLICK, LoadText);
function LoadText(Event:MouseEvent):void {
MyNotes.addEventListener(Event.SELECT, onFileSelected);
var swfTypeFilter:FileFilter = new FileFilter("Text Files","*.txt; .html;*.htm;*.php");
var allTypeFilter:FileFilter = new FileFilter("All Files (*.*)","*.*");
MyNotes.browse([swfTypeFilter, allTypeFilter]);
}
function onFileSelected(event:Event):void
{
trace("onFileSelected");
MyNotes.addEventListener(Event.COMPLETE, onFileLoaded);
MyNotes.load();
}
function onFileLoaded(event:Event):void
{
var fileReference:FileReference=event.target as FileReference;
var data:ByteArray=fileReference["data"];
TypeOwn_txt.text=data.toString();
}
The problem is I receive a "Symbol 'Structure Summary', Layer 'Actions', Frame 29, Line 19 1119: Access of possibly undefined property SELECT through a reference with static type flash.events:MouseEvent.
" in regards to the line "MyNotes.addEventListener(Event.SELECT, onFileSelected);". I've done some research and understand this is something to do with the parent not being identified as a MovieClip, or something along those lines. I'm still not sure, however I don't have a clue how to proceed! Thanks.
Just to let you know the problem is solved, although I really don't know how. I used this site for a template and worked backwards. I'll put in the working Code below in case it's of use to somebody else.
import flash.events.MouseEvent;
import flash.net.FileReference;
import flash.net.FileFilter;
import flash.utils.ByteArray;
import flash.events.MouseEvent;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.display.MovieClip;
var myNotes:FileReference;
Load_btn.addEventListener(MouseEvent.CLICK, onLoadClicked);
function onLoadClicked(event:MouseEvent):void
{
trace("onBrowse");
myNotes=new FileReference();
myNotes.addEventListener(Event.SELECT, onFileSelected);
var swfTypeFilter:FileFilter = new FileFilter("Text Files","*.txt; *.html;*.htm;*.php");
var allTypeFilter:FileFilter = new FileFilter("All Files (*.*)","*.*");
myNotes.browse([swfTypeFilter, allTypeFilter]);
}
function onFileSelected(event:Event):void
{
trace("onFileSelected");
myNotes.addEventListener(Event.COMPLETE, onFileLoaded);
myNotes.addEventListener(IOErrorEvent.IO_ERROR, onFileLoadError);
myNotes.load();
}
function onFileLoaded(event:Event):void
{
var fileReference:FileReference=event.target as FileReference;
var data:ByteArray=fileReference["data"];
textArea.text=data.toString();
myNotes.removeEventListener(Event.COMPLETE, onFileLoaded);
myNotes.removeEventListener(IOErrorEvent.IO_ERROR, onFileLoadError);
}
function onFileLoadError(event:Event):void
{
myNotes.removeEventListener(Event.COMPLETE, onFileLoaded);
myNotes.removeEventListener(IOErrorEvent.IO_ERROR, onFileLoadError);
trace("File load error");
}
Save_btn.addEventListener (MouseEvent.CLICK, SaveText);
function SaveText(Event:MouseEvent):void {
myNotes=new FileReference();
myNotes.save(textArea.text, "MyNotes.txt");
}
Thanks to everyone who contributed.

AS3 MovieClip not playing consistently

So at the beginning when my SWF loads up it also loads up a sequence of animated clips like so:
var loader:Loader = new Loader();
loader.load(new URLRequest("clips/clip4.swf"));
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, clip4Loaded);
And my clipLoaded function is:
private function clip4Loaded(e:Event):void {
clip4 = e.target.content as MovieClip;
}
The clip4 file being loaded in has a stop() at the first frame. Later in the game (clip4 is the "outro") I use:
clip4.gotoAndPlay(0);
clip4.addFrameScript(clip4.totalFrames - 1, clip4End);
However, the clip only seems to play about 25% of the time and all the other clips which I load the exact same way play fine. The only difference is that those clips get played fairly soon after they load which leads me to believe clip4 is being autoreleased at some point but I really have no clue.
strange - i've got a timeline-animated swf with stop(); in the first frame and the following code:
package {
import flash.display.Loader;
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.net.URLRequest;
/**
*
* #author www0z0k
*/
[SWF(width='400', height='300', frameRate='30')]
public class NewClass extends Sprite {
private const URL:String = 'res/1.swf';
private var spr:MovieClip;
public function NewClass():void {
var ldr:Loader = new Loader();
ldr.contentLoaderInfo.addEventListener(Event.INIT, onloaded);
ldr.load(new URLRequest(URL));
}
private function onloaded(e:Event):void {
spr = e.target.content as MovieClip;
addChild(spr);
spr.addFrameScript(spr.totalFrames - 1, function():void { x = x == 0 ? 100 : 0; } );
spr.addEventListener(MouseEvent.CLICK, onclick);
}
private function onclick(e:MouseEvent):void {
spr.gotoAndPlay(0);
}
}
}
and it works exactly as it's written.
could you please upload your clip4.swf anywhere (or test this code with it yourself)?