Access of undefined property errors - actionscript-3

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;
}
}
}

Related

FlashDevelop Syntax Error: Expecting Identifier Before Public

I'm new to AS3, and I would really appreciate any help you can give me! I am trying to create a button. However, FlashDevelop keeps telling me I have a Syntax Error in:
Line 11 Col. 2 (Syntax Error: expecting identifier before public)
Line 11 Col. 40 (Syntax Error: expecting identifier before extends)
Line 13 Col. 2 (Error: the private attribute may only be used on class property definitions)
Below is my code:
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.display.Graphics;
import flash.
/**
* ...
* #author 15leungjs1
*/
public class Main extends Sprite
{
private var button:Sprite;
public function Main():void
{
//Create a new instance of a Sprite to act as the button graphic.
button = new Sprite();
//Set the color of the button graphic
button.graphics.beginFill(0xFFAD3B);
//Set the X,Y, Width, and Height of the button graphic
button.graphics.drawRect(10, 0, 200, 100);
//Apply the fill
button.graphics.endFill();
//Add Handcursor,buttonMode, and mouseChildren
button.buttonMode = true;
button.useHandCursor = true;
button.mouseChildren = false;
//Add Button Sprite to stage
this.addChild(button);
}
}
}
}
The problem is simple...
You have the following line of code (line 6) that is not finished correctly:
import flash.
You need to complete that import statement or remove it completely.

AS3 Access of undefined property event

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;
}

error access of undefined event

i cant seem to go back to the first frame after the game over screen.
i get the error message Scene 1, 1119: Access of possibly undefined property Event through a reference with static type Class in frame 2 is it because i don't have a classset up in frame 1.
import flash.events.Event;
stop();
var isRight:Boolean=false
var isLeft:Boolean=false
var isUp:Boolean=false
var isDown:Boolean=false
stage.addEventListener(KeyboardEvent.KEY_DOWN, downKey);
function downKey(event:KeyboardEvent)
{
if(event.keyCode==39)
{
isRight=true
}
if(event.keyCode==37)
{
isLeft=true
}
if(event.keyCode==38)
{
isUp=true
}
if(event.keyCode==40)
{
isDown=true
}
}
ect...and the next frame code to go back to the previous one is
import flash.events.Event;
restart.addEventListener(Mouse.Event.MOUSE_UP,click)
function click(e:MouseEvent)
{
gotoAndStop(1);
}
you accidentally wrote Mouse.Event.MOUSE_UP instead of MouseEvent.MOUSE_UP in your addEventListener function

ActionScript 3 Flash error 1009 and 2025 combo box

Im trying to insert a music using combo box from the component asset. yesterday works fine, but today suddenly it stopped working and it gave me this error:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.
everytime I clicked on the combo box these error appears, strangely it works fine yesterday.
this is the code for the first frame:
import flash.events.Event;
import flash.events.MouseEvent;
import flash.system.fscommand;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.net.URLRequest;
import flash.events.Event;
import flash.media.SoundTransform;
import flash.events.MouseEvent;
//Declare MUSIC Variables
var musicSelection:Sound;
var musicChannel:SoundChannel;
var musicTrack:String = "music/dearly_beloved.mp3";
var musicVolume:Number=0.2;
/*var isMuted = false;*/
loadMusic();
function loadMusic():void
{
//first stop old sound playing
SoundMixer.stopAll();
musicSelection = new Sound();
musicChannel = new SoundChannel();
//create and load the required soun
musicSelection.load(new URLRequest(musicTrack));
//when loaded - play it
musicSelection.addEventListener(Event.COMPLETE, musicLoaded);
}
function musicLoaded(evt:Event):void
{
//finished with this listener
musicSelection.removeEventListener(Event.COMPLETE, musicLoaded);
//play music
playMusic();
}
function playMusic():void
{
//play the random or selected music
musicChannel = musicSelection.play();
//setting the volume control property to the music channel
musicChannel.soundTransform = new SoundTransform(musicVolume, 0);
//but add this one to make repeats
musicChannel.addEventListener(Event.SOUND_COMPLETE, playAgain);
}
function playAgain(evt:Event):void {
// remove this listener and repeat
musicChannel.removeEventListener(Event.SOUND_COMPLETE, playAgain);
playMusic();
}
and this is the code for the second frame:
import flash.events.Event;
menuBtn.addEventListener(MouseEvent.CLICK, goToMenu)
function goToMenu(evt:Event):void
{
gotoAndPlay(2);
}
// BUTTON EVENT LISTENERS
musicCombo.addEventListener(Event.CHANGE, updateMusic);
volumeSL.addEventListener(Event.CHANGE, setSlider);
//process COMBO BOX changes
function updateMusic(evt:Event):void
{
if (musicCombo.selectedItem.data == "none")
{
//no music is required so stop sound playing
SoundMixer.stopAll();
}
else
{
//otherwise load in the selected music
musicTrack = "music/" + musicCombo.selectedItem.data;
loadMusic();
}
}
function setSlider(evt:Event):void
{
//identify the button clicked
var mySlider:Object = (evt.target);
//adjusting to volume of the music channel
musicVolume = mySlider.value;
musicChannel.soundTransform = new SoundTransform(musicVolume, 0);
}
the error occurs whenever I clicked on the combo box and when I tried to insert another combo box without putting any data, the same error occurs. hope you guys can help me ASAP cause this is due on friday this week xD
thanks
Alright, found the answer. It seems I need to put these 3 lines of code:
import fl.events.ComponentEvent;
import fl.controls.ComboBox;
import fl.data.DataProvider;

How to convert a Shape to MovieClip using ActionScript 3.0 only

import flash.display.Shape;
import flash.display.Graphics;
stage.addEventListener(Event.ENTER_FRAME, startAnim);
function startAnim(e:Event):void
{
var shape1:Shape = new Shape();
shape1.graphics.beginFill(0x333333,1);
shape1.graphics.drawRect(40,50,250,125);
shape1.graphics.endFill();
addChild(shape1); // this will add a shape of rectangle to stage
}
This is a very simple function creating a rectangle shape on stage. Ok but the problem is how can I convert this SHAPE to MOVIECLIP using ActionScript only so I can add Events to the same (shape1).
hmmm by using a MovieClip instead of a Shape. a MovieClip also has a Graphics object.
import flash.display.MovieClip ;
//import flash.display.Graphics;//not needed
//stage.addEventListener(Event.ENTER_FRAME, startAnim); //remove enterframe
//function startAnim(e:Event):void { //no need for a handler
var shape1:MovieClip = new MovieClip();
shape1.graphics.beginFill(0x333333,1);
shape1.graphics.drawRect(40,50,250,125);
shape1.graphics.endFill();
addChild(shape1); // this will add a MovieClip of rectangle to stage
shape1.addEventListener(MouseEvent.MOUSE_DOWN, dragShape);
function dragShape(E:MouseEvent)
{
shape1.startDrag()
}
shape1.addEventListener(MouseEvent.MOUSE_UP, dropShape);
function dropShape(E:MouseEvent)
{
shape1.stopDrag()
}
//} no need for that either :)
beware that, as such, your function is called on ENTER_FRAME = 25 or more times per second, therefore you'll create and add a clip to stage 25 or more times per second
+ the reference is created locally, in the function, so you won't be able to access "shape1" from outside, once your object is created.
I don't think you can convert a Shape to a MovieClip. What you can do is to create a MovieClip class, and in the constructor generate the Shape object, and add it to the MovieClip.
public class Car extends MovieClip {
private var shape1:Shape = new Shape();
public function Car() {
shape1.graphics.beginFill(0x333333,1);
shape1.graphics.drawRect(40,50,250,125);
shape1.graphics.endFill();
addChild(shape1); // this will add a shape of rectangle to stage
}
}
Shape has also events.
activate
added
addedToStage
deactivate
enterFrame
removed
removedFromStage
render
But since it doesn't extends from InteractiveObject, you can't handle input.