Reputation: 7
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
Upvotes: 0
Views: 236
Reputation: 6751
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.
Upvotes: 2