Copy link to clipboard
Copied
Hi!
I just got started using animate cc with AS3 and i am having a bit of trouble with some script/code issues. My problem is the following:
I have a multi-scene project which has already some actions in it (navigation between scenes) and I want to import a prewritten .as file to the project so I can create a game in one the scenes. When I class the project in properties with the name of the .as document so the game scene can play I get multiple errors an loose all the event clicks that enabled me to navigate between scenes. How do I solve this? What is the correct way of inserting "external" code in a project?
Any help will be highly valueable
Thanks!
Diego
Copy link to clipboard
Copied
Hi, Diego!
First, make sure you are typing the name of the class in the Properties panel without the .as extension.
And if your class is inside of any folder, you have to first include the parent folder of your class files in the source path settings (inside of the Advanced ActionScript 3.0 Settings window) and also set the correct package location in every class.
For example: if you have a folder called 'src' in the same folder as your FLA and inside this 'src' folder you have a folder called 'system' and inside of this 'system' folder you have another one called 'core', you would have to go to the source path settings and set the 'src' folder to be the container of your class files, like this:
And if inside the 'core' folder you have a class called 'Main.as', its basic structure would be like this:
package system.core
{
import flash.display.MovieClip;
public class Main extends MovieClip
{
public function Main()
{
trace("working");
}
}
}
And to make the 'Main.as" the document class, type in the Properties panel: system.core.Main.
But if you got all this set correctly, another possibility for the errors may be a conflict with the code inside of your class and the code in the timeline.
I hope it helps!
Regards,
JC
Copy link to clipboard
Copied
Hi JC,
Thank you very much for your quick response. I got all the document class issue solved correctly (just as you laid out previously). The problem is that when I reproduce/publish the project I keep getting the same errors:
Multiple errors 1046: Type was not found or was not a compile-time constant: MouseEvent.
When the Main.as class is incorporated come of the previous actions (buttons for navigation between scenes) added to the project no longer seem to work. Is there something I am doing wrong?
Any help will be valuable.
Best regards
Diego
Here is the code from Main.as and actions inserted previously in the project:
Main.as:
package {
import flash.display.MovieClip;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.events.Event; //used for ENTER_FRAME event
public class Main extends MovieClip {
const speed: int = 20; //speed of the snake
var score: int;
var vx: int;
var vy: int;
var gFood: Food;
var head: SnakePart;
var SnakeDirection: String;
var snake: Array;
public function Main() {
init();
}
function init(): void {
//Initialize everything!
vx = 1;
vy = 0;
score = 0;
snake = new Array();
SnakeDirection = "";
//add food to the stage
addFood();
//add snakes head to the stage
head = new SnakePart();
head.x = stage.stageWidth / 2;
head.y = stage.stageHeight / 2;
snake.push(head);
addChild(head);
stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
addEventListener(Event.ENTER_FRAME, onEnterFrame);
//ENTER_FRAME listener is attached to main class and not to the stage directly
}
//This function will add food to the stage
function addFood(): void {
gFood = new Food();
gFood.x = 50 + Math.random() * (stage.stageWidth - 100);
gFood.y = 50 + Math.random() * (stage.stageHeight - 100);
addChild(gFood);
}
//this function will reset the game
function reset(): void {
removeChild(gFood);
addFood();
head.x = stage.stageWidth / 2;
head.y = stage.stageHeight / 2;
vx = 1;
vy = 0;
for (var i = snake.length - 1; i > 0; --i) {
removeChild(snake);
snake.splice(i, 1);
}
}
function onKeyDown(event: KeyboardEvent): void {
if (event.keyCode == Keyboard.LEFT) {
SnakeDirection = "left";
} else if (event.keyCode == Keyboard.RIGHT) {
SnakeDirection = "right";
} else if (event.keyCode == Keyboard.UP) {
SnakeDirection = "up";
} else if (event.keyCode == Keyboard.DOWN) {
SnakeDirection = "down";
}
}
function onKeyUp(event: KeyboardEvent): void {
if (event.keyCode == Keyboard.LEFT) {
SnakeDirection = "";
} else if (event.keyCode == Keyboard.RIGHT) {
SnakeDirection = "";
} else if (event.keyCode == Keyboard.UP) {
SnakeDirection = "";
} else if (event.keyCode == Keyboard.DOWN) {
SnakeDirection = "";
}
}
function onEnterFrame(event: Event): void {
//setting direction of velocity
if (SnakeDirection == "left" && vx != 1) {
vx = -1;
vy = 0;
} else if (SnakeDirection == "right" && vx != -1) {
vx = 1;
vy = 0;
} else if (SnakeDirection == "up" && vy != 1) {
vx = 0;
vy = -1;
} else if (SnakeDirection == "down" && vy != -1) {
vx = 0;
vy = 1;
}
//collison with stage
if (head.x - head.width / 2 <= 0) {
score = 0;
reset();
}
if (head.x + head.width / 2 >= stage.stageWidth) {
score = 0;
reset();
}
if (head.y - head.height / 2 <= 0) {
score = 0;
reset();
}
if (head.y + head.height / 2 >= stage.stageHeight) {
score = 0;
reset();
}
//move body of the snake
for (var i = snake.length - 1; i > 0; --i) {
snake.x = snake[i - 1].x;
snake.y = snake[i - 1].y;
}
//changing the position of snake's head
head.x += vx * speed;
head.y += vy * speed;
//collision with tail
for (var i = snake.length - 1; i >= 1; --i) {
if (snake[0].x == snake.x && snake[0].y == snake.y) {
reset();
break;
}
}
//collision with food
if (head.hitTestObject(gFood)) {
score += 1;
removeChild(gFood);
addFood();
var bodyPart = new SnakePart();
bodyPart.x = snake[snake.length - 1].x;
bodyPart.y = snake[snake.length - 1].y;
snake.push(bodyPart);
addChild(bodyPart);
}
//display scores
txtScore.text = String(score);
}
}
}
Navigation actions in the project:
Scene 1:
btn_start.addEventListener(MouseEvent.CLICK, fl_ClickToGoToScene);
function fl_ClickToGoToScene(event:MouseEvent):void
{
MovieClip(this.root).gotoAndPlay(1, "Escena 4");
}
Scene 2:
btn_inicio.addEventListener(MouseEvent.CLICK, fl_ClickToGoToScene_2);
function fl_ClickToGoToScene_2(event:MouseEvent):void
{
MovieClip(this.root).gotoAndPlay(1, "Escena 1");
}
btn_texto_omran.addEventListener(MouseEvent.CLICK, fl_ClickToGoToScene_8);
function fl_ClickToGoToScene_8(event:MouseEvent):void
{
MovieClip(this.root).gotoAndPlay(1, "Escena 4");
}
Copy link to clipboard
Copied
You need an import line for each type of event you're going to be using. The keyboard event works because you have this line:
import flash.events.KeyboardEvent;
for the mouse events to work you would need this line too:
import flash.events.MouseEvent;
Copy link to clipboard
Copied
Hi Colin and JC,
Thank you once again for your fast help! But I added "import flash.events.MouseEvent;" to de Main.as document and I am still getting the same error. Mouse events on the projects timeline still don´t seem to work. I must bee missing something obvious. Any other suggestions? Thank you once again for you patience and help
Best regards
Diego
Copy link to clipboard
Copied
Colin is right.
Also, you should rename your onKeyDown and onKeyUp handlers to something like onKeyDownHandler and onKeyUpHandler to not get compiler warnings.
You also have a duplicate variable definition on the line
for (var i = snake.length - 1; i >= 1; --i) {
Change to:
for (i = snake.length - 1; i >= 1; --i) {
Copy link to clipboard
Copied
Hi Colin and JC,
Thank you once again for your fast help! But I added "import flash.events.MouseEvent;" to de Main.as document and I am still getting the same error. Mouse events on the projects timeline still don´t seem to work. I must bee missing something obvious. Any other suggestions? Thank you once again for you patience and help
Best regards
Diego
Copy link to clipboard
Copied
You're welcome.
Add the "import flash.events.MouseEvent;" directive to the first line of the code in the timeline because the mouse events are being used there.
Copy link to clipboard
Copied
This is what I have for the actions window. I introduced "import flash.events.Mouse.Event;" but even now it will not work. I now get other errors. I just noticed the rest of the actions of the timeline also dont work (for example, stops). Is it possible that once you class the project with a Main.as archive the actions in the timeline stop working or are no longer compatible?
Regards
Diego
Copy link to clipboard
Copied
Do any errors show in the Compiler Errors panel? What errors are showing in the Output panel?
Copy link to clipboard
Copied
It's because one single error can mess up even things that are correct.
BTW, did you fix the problems I pointed out in the comment #4?
Copy link to clipboard
Copied
Hi again:
Errors are the same as at the begining. And yes, I corrected all the code errors. It still does not work. I think I am going to start over again. Something must be out of place.
Thank you anyway!
Best regards
Diego
Copy link to clipboard
Copied
Hi again, Diego!
I think there's no need to start over.
Would you mind sharing your file?