AS3 Errors 1180 and 1046 - actionscript-3

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

Related

AS3: graphics API not working as expected

I've been working on an AS3 project, but since I usually work in php/javascript, this language is new to me and I have come across a problem that I can't figure out how to solve. I thought i'd ask it here because it's probably super simple and i just missed something (really) basic.
Here's my code in my external AS3 file:
package {
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.events.Event;
import flash.display.Shape;
import flash.display.Graphics;
public class lightstage_debug extends MovieClip {
//create drag functions for tools
public function dragMirror1(event:MouseEvent):void
{
if (stage.contains(mirror1))
{
mirror1.startDrag();
}
}
public function releaseMirror1(event:MouseEvent):void
{
if (stage.contains(mirror1))
{
mirror1.stopDrag();
}
}
/*********************************************************************************************
TODO: make mirror2, mirror3 etc drag function exist in code even when those mirrors don't exist
**********************************************************************************************/
public function reCheck(event:Event):void
{
//BEGIN LEVEL 1 RECHECK//
if (level.number == 1)
{
//check if mirror1 is touching line1
if (mirror1.hitTestObject(line1))
{
//redraw line 1 to stop at mirror1
/*************************************************************************************
TODO: MAKE LINE1 END **EXACTLY** WHERE MIRROR1 TOUCHES IT
**************************************************************************************/
line1.graphics.clear();
line1.graphics.lineStyle(2,0x000000,1);
line1.graphics.moveTo(0,200);
line1.graphics.lineTo((mirror1.x + (mirror1.width/2)),200);
//redraw line 2 to start at mirror1
line2.graphics.clear();
line2.graphics.lineStyle(2,0x000000,1);
line2.graphics.moveTo((mirror1.x + (mirror1.width/2)),200);
line2.graphics.lineTo(0,200);
lines.addChild(line2);
//show line2
//line2.visible = true;
//line2.alpha = 100;
trace('line didnt appear');
}
//run this if line1 is not touching mirror1
else
{
//redraw line 1 to reach the end of the stage
line1.graphics.clear();
line1.graphics.lineStyle(2,0x000000,1);
line1.graphics.moveTo(0,200);
line1.graphics.lineTo(550,200);
//hide line2
//line2.alpha = 0;
}
}
//END LEVEL 1 RECHECK//
}
}
}
Here is the code in my .FLA file:
//import libraries
import flash.display.Shape;
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.events.Event;
import flash.display.Graphics;
//create variables
var globe1:MovieClip = new globe;
var mirror1:MovieClip = new mirror;
//create display containers
var lines:MovieClip = new MovieClip;
var mirrors:MovieClip = new MovieClip;
var concaves:MovieClip = new MovieClip;
var convexes:MovieClip = new MovieClip;
var globes:MovieClip = new MovieClip;
//create shapes for this level
var line1:Shape = new Shape;
var line2:Shape = new Shape;
//add display containers to the stage
addChild(lines);
addChild(mirrors);
addChild(concaves);
addChild(convexes);
addChild(globes);
//create level object to store level properties
var level:Object = new Object;
//create scene properties
level.mirrors = 1;
level.concaves = 0;
level.convexes = 0;
level.globes = 1;
level.number = 1;
//add all lines to the stage
lines.addChild(line1);
lines.addChild(line2);
//draw line1
line1.graphics.clear();
line1.graphics.lineStyle(2, 0x000000, 1);
line1.graphics.moveTo(0,200);
line1.graphics.lineTo(550, 200);
//4DEBUG: testing to see why line2 dosent appear
line2.graphics.clear();
//add mirrors
mirrors.addChild(mirror1);
//add concaves
//add convexes
//add globes
globes.addChild(globe1);
//position mirrors
mirror1.x = 340;
mirror1.y = 300;
//position globes
globe1.x = 125;
globe1.y = 50;
//create listeners
mirror1.addEventListener(MouseEvent.MOUSE_DOWN, dragMirror1);
mirror1.addEventListener(MouseEvent.MOUSE_UP, releaseMirror1);
stage.addEventListener(Event.ENTER_FRAME, reCheck);
My stage is empty and I only have one frame at the moment. The globe and mirror movieclips are in my library with the AS3 linkage names I used to create them in the code.
I'm trying to create something like described at http://raphaelhennessy.com/misc/Explanation.png (ignore everything below the text 'LightStage Explanation') - but my problem is that although i can get the initial line (line1) to 'shrink' and stop at the mirror when the mirror is placed on top of the line, line2 does not appear and start at the mirror, going upwards to the top of the stage like intended.
Thanks in advance,
-Raph
It looks like you want line2.graphics.lineTo(0,200); to be line2.graphics.lineTo((mirror1.x + (mirror1.width/2)), 0);. Right now you're just drawing the lines on top of each other, in opposite directions.

AS3: How do I call a function with two arguments - (vBox, and vFile)

Adobe Flash CC
Sort of confused here. I'm working on optimizing my code so that instead of calling launchVideo(); for every single video I can can simply call it once, while passing a new source string to the function.
How do I call the launchVideo function from within another function?
When I add an event listener, which calls the playMPMovie
buttonOne.addEventListener(MouseEvent.MOUSE_DOWN, playMPMovie, false, 0, true);
function playMPMovieOne(): void {
video_file = "/videos/MP_01.mp4";
launchVideo();
}
I get this...
Scene 1, Layer 'actions', Frame 1, Line 21, Column 2 1136: Incorrect number of arguments. Expected 2.
When I try adding (vBox, vFile) to launchVideo(); I get this...
Scene 1, Layer 'actions', Frame 1, Line 20, Column 20 1120: Access of undefined property vFile.
Scene 1, Layer 'actions', Frame 1, Line 20, Column 14 1120: Access of undefined property vBox.
Here is the full code.
stop();
import flash.events.MouseEvent;
import flash.events.Event;
import flash.display.MovieClip;
import flash.display.Graphics;
import fl.video.*;
vinetteMC.visible = false;
// VARIABLES //
var video_holder: MovieClip = new MovieClip();
var video_file: String;
// EVENT LISTENERS //
buttonOne.addEventListener(MouseEvent.MOUSE_DOWN, playMPMovie, false, 0, true);
function playMPMovieOne(): void {
video_file = "/videos/MP_01.mp4";
launchVideo();
}
// Place Playback
function launchVideo(vBox, vFile): void {
var flvPlayer: FLVPlayback = new FLVPlayback();
import fl.video.*;
import flash.events.*;
flvPlayer.source = vFile;
flvPlayer.skinAutoHide = true;
flvPlayer.skinBackgroundColor = 0x000000;
flvPlayer.width = 1920;
flvPlayer.height = 1080;
flvPlayer.addEventListener(Event.COMPLETE, completeHandler, false, 0, true);
function completeHandler(event: Event): void {
removeChild(video_holder);
removeChild(flvPlayer);
flvPlayer.addEventListener(fl.video.VideoEvent.COMPLETE, completeHandler, false, 0, true);
trace("Complete handler called");
}
vBox.addChild(flvPlayer);
}
launchVideo(video_holder, video_file);
It looks like you are wanting to call
function playMPMovieOne(): void {
video_file = "/videos/MP_01.mp4";
launchVideo(video_holder, video_file)
}
Also from your example code the video holder is created but never added to the display list. You may need to add this to see anything.
addChild(video_holder);

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

AS3 Getting error 5001 when trying to import another .as file

I;m new to AS3 and still trying wrap my head around it.
I'm trying to program a very simple platformer. At present, I'm trying to make the player appear to be moving by means of moving the background behind him. So, I've made a separate .as file to contain the simple logic for making the background move. It's title is BG.as and I've been able to import a file with that exact name before without any issue on that front, but now it is being temperamental with me. I've gone into my AS3 Pref's in flash and set the file paths for the root folder, the folder containing the root folder, and even the folder holding the documents themselves. Both the .as file and the .fla file are in the same folder, but still I get an Error 5001 declaring that the name of BG does not reflect the file of the location. I've already tried writing out the file path as the name of the package and so on and so forth. If anyone has any idea of what the issue may be, I would appreciate it.
Here is my code.
import movingBackground.BG.*;
This is just the import statement in my .fla file. movingBackground is the name of the folder it is in.
package BG
{
import flash.events.EventDispatcher;
import flash.events.Event;
public class BG
{
public function loop(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;
}
}
stage.addEventListener(Event.ENTER_FRAME, loop);
}
}
This is my BG.as file.
~~~~~EDIT::~~~~~~~
Okay, so I've renamed things and the renaming seems to have sorted out the Error 5001, but there is a new issue. Now it can't seem to figure out that the class I want to import exists.
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.
with that said, here is what I've edited 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 the newly renamed thebackground.as
Your package in the class does not reflect your import.
The package in your class file definition should be movingBackground.BG, otherwise you are going to get an error saying it doesn't match.
It's also not a good idea to name your package the same as your class, if for nothing else but for the sake of avoiding confusion.
You also don't have a constructor for your BG class, that I can see.
Definitely take a look at the first comment on this answer, as it helps to utilize standard naming conventions. There is value in following them.