ActionScript 3 Error: 1083: Syntax error: package is unexpected - actionscript-3

Hi I'm currently learning how to use Flash so I'm creating a colouring book as a project I'm just having trouble with my code though. The Code I've written is:
import coloring;
PrevBtn.addEventListener(MouseEvent.CLICK, fl_ClickToGoToAndStopAtFrame_14);
function fl_ClickToGoToAndStopAtFrame_14(event:MouseEvent):void
{
gotoAndStop(40);
}
HomeBtn.addEventListener(MouseEvent.CLICK, fl_ClickToGoToAndStopAtFrame_11);
function fl_ClickToGoToAndStopAtFrame_11(event:MouseEvent):void
{
gotoAndStop(10);
}
NextBtn.addEventListener(MouseEvent.CLICK, fl_ClickToGoToAndStopAtFrame_13);
function fl_ClickToGoToAndStopAtFrame_13(event:MouseEvent):void
{
gotoAndStop(20);
}
HelpBtn.addEventListener(MouseEvent.CLICK, fl_ClickToGoToAndStopAtFrame_31);
function fl_ClickToGoToAndStopAtFrame_31(event:MouseEvent):void
{
gotoAndStop(45);
}
Any help would be majorly appreciated. Thanks in advance!
**UPDATE:
So I have created coloring.as and now import it in my code but I am now getting a further error when I get to my colouring sheet which reads:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at laurenBurke/frame15()
at flash.display::MovieClip/gotoAndStop()
at laurenBurke/fl_ClickToGoToAndStopAtFrame_4()**
The coloring.as file contains the following code
package {
import flash.display.Sprite;
import flash.geom.ColorTransform;
import flash.events.MouseEvent;
public class coloring extends Sprite {
public var Design1:Design1 = new Design1();
public var palette:palette_mc;
public var pal_color:ColorTransform;
public var colors:Array=new Array(0x000000,0xFFFFFF,0xFF0000,0xFF4040,0x333399,
0x99CCCC,0xFFCC00,0xFFFF66,0x33CC00,0x99FF66,
0x660099,0x9933FF,0xFF8000,0xFF9966,0xFF0099,
0xFF99CC,0x0099CC,0x6699CC,0x666666,0xCCCCCC,
0x003366,0x000033,0x99CC99,0x4E9A4E,0x9966FF,
0x990066);
public var current_color:int=0;
public function coloring():void {
addChild(Design1);
Design1.addEventListener(MouseEvent.CLICK,on_Design1_click);
for (var i:int=0; i<26; i++) {
palette = new palette_mc();
pal_color=palette.transform.colorTransform;
pal_color.color=colors[i];
palette.transform.colorTransform=pal_color;
palette.x=40+i*60;
palette.y=300;
palette.ind=i;
addChild(palette);
palette.addEventListener(MouseEvent.CLICK,on_palette_click);
}
}
public function on_palette_click(e:MouseEvent):void {
var palette_clicked:palette_mc=e.currentTarget as palette_mc;
current_color=palette_clicked.ind;
}
public function on_Design1_click(e:MouseEvent):void {
for (var i:int = 0; i < Design1.numChildren; i++) {
if (Design1.getChildAt(i).hitTestPoint(mouseX,mouseY,true)) {
pal_color=Design1.getChildAt(i).transform.colorTransform;
pal_color.color=colors[current_color];
Design1.getChildAt(i).transform.colorTransform=pal_color;
}
}
}
}
}

declaring Design1 with the same identifier as the class itself here:
public var Design1:Design1 = new Design1();
... is creating an ambiguous reference. Later on when you call addChild(Design1); you're potentially trying to add the class object itself, not an instance, to the stage. Try changing the var name to camel-case.

Related

5006: An ActionScript file can not have more than one externally visible definition

So, I'm having this error with AS3. I have an object(movie clip) called Inimigo2_Caique2, and his is called Inimigo2_Caique2, too. (I don't know why, but the icon of the object in the library is green instead of blue).
When I try to run the file, I get this error message:
5006: An ActionScript file can not have more than one externally visible definition: Inimigo2_Caique2, removeListeners
I have other object called Inimigo_Caique2, and another class called Inimigo_Caique2. (The icon is green too.)
Here is the code of my Inimigo2_Caique2 class:
package{
import flash.events.Event;
import flash.display.MovieClip;
import flash.display.Sprite;
public class Inimigo2_Caique2 extends Sprite{
private var palco:Object;
private var yd:Number;
private var xd:Number;
public function Inimigo2_Caique2(){
addEventListener(Event.ADDED_TO_STAGE,inicia2);
}
private function inicia2(e:Event){
palco=MovieClip(root);
yd=palco.aviao.y-y;
xd=palco.aviao.x-x;
addEventListener(Event.ENTER_FRAME,loop1);
}
private function loop1(e:Event){
var angulo:Number=Math.atan2(yd,xd);
x+=Math.cos(angulo)*10;
y+=Math.sin(angulo)*10;
for(var i:int = 0; i<palco.recipiente.numChildren;i++){
var alvoBala2:Sprite = palco.recipiente.getChildAt(i);
var ris:Number=alvoBala2.y-y;
var run:Number=alvoBala2.x-x;
var dis:Number=Math.sqrt(Math.pow(ris,2)+Math.pow(run,2));
if(dis<100){
if(run<0){
x+=20;
}else{
x-=20;
}
}
}
if(hitTestObject(alvoBala2)){
palco.recipiente.getChildAt(i).removeListeners();
palco.recipiente.removeChild(alvoBala2);
palco.Som2.play();
var boom3:MovieClip = new explosao();
boom3.x=x;
boom3.y=y;
stage.addChild(boom3);
palco.pontos+=300;
var textopontos=String(palco.pontos);
palco.txt_pontos.text=textopontos;
removeEventListener(Event.ENTER_FRAME,loop1);
palco.removeChild(this);
}
}
if(hitTestObject(palco.aviao)){
palco.Som2.play();
var aviaoboom:MovieClip = new explosao();
aviaoboom.x=palco.aviao.x;
aviaoboom.y=palco.aviao.y;
stage.addChild(aviaoboom);
palco.gotoAndStop(2);
}
}
public function removeListeners():void{
removeEventListener(Event.ENTER_FRAME,loop1);
}
}
I don't know why this is happening. I've already checked the brackets and everything, but nothing works.
Thanks in advance. This error is driving me mad.
In the following case, the function myFunction is outside of the public class Main and interpreted as another class by the compiler:
My class
package
{
import flash.display.MovieClip;
public class Main extends MovieClip
{
}
//
public function myFunction()
{
}
//
}
My Fla
myFunction(); // function invoked
This error message occurs: 5006: An ActionScript file can not have more than one externally visible definition: Main, myFunction, because the correct code is:
My class
package
{
import flash.display.MovieClip;
public class Main extends MovieClip
{
public function myFunction()
{
trace('myFunction should be here');
}
}
}
So your error message indicates that your function removeListeners is outside of your class Inimigo2_Caique2
Remark
As MasterRoro says, this block seems to be outside your loop1 function:
if (hitTestObject(palco.aviao)) {
palco.Som2.play();
var aviaoboom:MovieClip = new explosao();
aviaoboom.x=palco.aviao.x;
aviaoboom.y=palco.aviao.y;
stage.addChild(aviaoboom);
palco.gotoAndStop(2);
}
it seems that your block:
if(hitTestObject(palco.aviao)){
palco.Som2.play();
var aviaoboom:MovieClip = new explosao();
aviaoboom.x=palco.aviao.x;
aviaoboom.y=palco.aviao.y;
stage.addChild(aviaoboom);
palco.gotoAndStop(2);
}
} //Extra curly brace
Is supposed to go inside of your loop1(e:Event) function. You terminated the loop1 function earlier than this with 2 curly braces, and then the extra curly brace in said block is terminating your class definition, which is why the compiler is throwing that error.
Try moving this block inside of the loop1() function and removing the extra curly brace at the end.

Actionscript 3: Access movieclips from array with for-loop

I am having some trouble with accessing the movieclips I've added as childs. From what I've read, a way to solve this is to add every new movieclip to an array, and then loop through this array when I want to change something about all the movieclips.
In my case I want to scale them.
This is how I've tried to implement this function:
package
{
import flash.display.MovieClip;
import flash.utils.Dictionary;
import flash.events.MouseEvent;
import Airport;
public class Main extends Sprite
{
public var tile:MovieClip
public var bg_image:Sprite;
public var airport:Airport;
public static var airportDict:Dictionary = new Dictionary();
public static var collectedAirportArray:Array = new Array();
public function Main()
{
addEventListener(Event.ADDED_TO_STAGE, init);
}
public function init(e:Event):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
// airportDict is being filled up here.. but that's not really relevant for my problem
bg_image = new Image();
addChild(bg_image);
bg_image.addEventListener(MouseEvent.CLICK, testfunction);
for (var k:Object in airportDict)
{
var airport:Airport = new Airport(k,airportDict[k]["x"], airportDict[k]["y"]);
collectedAirportArray.push(collectedAirportArray);
bg_image.addChild(airport);
}
}
private function testfunction(evt:MouseEvent):void
{
for each (tile in collectedAirportArray)
{
tile.scaleY = 2 * tile.scaleY;
}
}
}
}
This give me the error message TypeError: Error #1034: Type Coercion failed: cannot convert []#30977eb1 to flash.display.MovieClip.
at Main/testfunction() when clicking on the bg_image
You have an error in where you are making the array. You have there collectedAirportArray.push(collectedAirportArray); - you're basically pushing an array into itself. Nice snakey, they said. You should instead push the newly created airport in there:
collectedAirportArray.push(airport);
If your airport is any kind of DisplayObject your code should work.
What AirPort class is extending? If it extends MovieClip then try type casting like so:
private function testfunction(evt:MouseEvent):void
{
for each (tile in collectedAirportArray)
{
var myTile:MovieClip = MovieClip(tile);
myTile.scaleY = 2 * myTile.scaleY;
}
}

How to get a Numeric Stepper and sliders to work together in Flash CS4 AS3

Hi I am working on a piece of coursework for school and I want to have a page where I have sliders that reflect a numeric stepper and vice versa however whenever I find a tutorial online it keeps giving me errors, the most recent of which is 1120: Access of undefined property NumericStepperEvent. I am hesitant to continue with the rest of the steppers because of the fact that I am unsure how to get it to work but I need to have this section done by Friday. Any help will be much appreciated.
Thanks Matt
package
{
import flash.display.MovieClip;
import flash.events.MouseEvent;
import fl.controls.Slider;
import fl.events.SliderEvent;
import fl.controls.Label;
import fl.controls.NumericStepper;
public class Main extends MovieClip
{
//Variables
var startPage:StartPage;
var tutorialPage:TutorialPage;
var maleSizePage:MaleSizePage;
var femaleSizePage:FemaleSizePage;
public function Main()
{
//Pages
startPage = new StartPage ;
tutorialPage = new TutorialPage ;
maleSizePage = new MaleSizePage ;
femaleSizePage = new FemaleSizePage ;
addChild(startPage);
//Event Listeners
startPage.startButton.addEventListener(MouseEvent.CLICK,startButtonClick);
tutorialPage.continueButton.addEventListener(MouseEvent.CLICK,tutorialContinueButtonClick);
maleSizePage.heightSlider.addEventListener(SliderEvent.CHANGE,heightSlChange_M);
maleSizePage.neckSlider.addEventListener(SliderEvent.CHANGE,neckSlChange_M);
maleSizePage.chestSlider.addEventListener(SliderEvent.CHANGE,chestSlChange_M);
maleSizePage.waistSlider.addEventListener(SliderEvent.CHANGE,waistSlChange_M);
maleSizePage.armSlider.addEventListener(SliderEvent.CHANGE,armSlChange_M);
maleSizePage.legSlider.addEventListener(SliderEvent.CHANGE,legSlChange_M);
maleSizePage.heightValue.addEventListener(NumericStepperEvent.CHANGE,heightVChange_M);
}
function startButtonClick(event:MouseEvent):void
{
addChild(tutorialPage);
}
function tutorialContinueButtonClick(event:MouseEvent):void
{
if (startPage.maleSelection.selected == true)
{
addChild(maleSizePage);
removeChild(startPage);
removeChild(tutorialPage);
}
if (startPage.femaleSelection.selected == true)
{
addChild(femaleSizePage);
removeChild(startPage);
removeChild(tutorialPage);
}
}
function heightSlChange_M(Event:SliderEvent):void
{
maleSizePage.heightValue.value = maleSizePage.heightSlider.value;
}
function neckSlChange_M(Event:SliderEvent):void
{
maleSizePage.neckValue.value = maleSizePage.neckSlider.value;
}
function chestSlChange_M(Event:SliderEvent):void
{
maleSizePage.chestValue.value = maleSizePage.chestSlider.value;
}
function waistSlChange_M(Event:SliderEvent):void
{
maleSizePage.waistValue.value = maleSizePage.waistSlider.value;
}
function armSlChange_M(Event:SliderEvent):void
{
maleSizePage.armValue.value = maleSizePage.armSlider.value;
}
function legSlChange_M(Event:SliderEvent):void
{
maleSizePage.legValue.value = maleSizePage.legSlider.value;
}
function heightVChange_M(Event:NumericStepper)
{
maleSizePage.heightSlider.value = maleSizePage.heightValue.value;
}
}
}
You need to tell the compiler which classes to use. At the moment it has no idea what NumericStepperEvent is.
Add at the top with other imports add: import mx.events.NumericStepperEvent;
Also i'd recommend changing the argument variables written Event:SliderEvent to lowercase event:SliderEvent as this may cause another compiler error.

Error #1010 as3

How do I fix this Error #1010: : A term is undefined and has no properties. at Main/showIntro() at Main().
The code that I used is from a online tutorial.
package {
import flash.events.MouseEvent;
import flash.events.Event;
import flash.display.MovieClip;
public class Main extends MovieClip {
private var intro:Introduction;
public function Main() {
intro = new Introduction();
showIntro();
}
private function showIntro():void {
//add intro
addChild(intro);
//add eventlistener
intro.begin_btn.addEventListener(MouseEvent.CLICK, clickBegin);
intro.x = stage.stageWidth/2;
intro.y = stage.stageHeight/2;
}
private function clickBegin(e:MouseEvent):void {
trace("0");
}
}
}
}
I'm pretty sure this is because begin_btn isn't defined in the Introduction class when you instantiate it.
Try adding a definition in the class like so:
public var begin_btn:MovieClip;
If you have defined it, check that it is a publicly accessible

Error #1034, with a MouseEvent

I am making a basic point n' click game and I came upon this error:
TypeError: Error #1034: Type Coercion failed: cannot convert 3 to cem.mouvement.
Here's my script:
package cem {
import flash.events.Event;
import flash.display.MovieClip;
import cem.microjeux.events.InfoJeuEvent;
import cem.mouvement;
import flash.events.MouseEvent;
public class monterJeu extends MovieClip
{
private static var pType:String = "type";
private static var pNom:String = "testNom";
private static var pCourriel:String = "test#hotmail.com";
private static var pDifficulte:int = 0;
private static var pLangue:int = 0;
private static var pTitre:String = "Veuillez sortir";
private static var pVersion:String = "1.5";
private static var pCoordonnees:Number;
private var environnementJeu:environnement = new environnement();
private var personnageJeu:personnage = new personnage();
public function monterJeu():void
{
jouer(pNom,pDifficulte,pLangue);
dispatchEvent(new InfoJeuEvent(pType,pNom,pCourriel,pTitre,pVersion));
stage.addEventListener(MouseEvent.CLICK, test);
}
public function jouer(PNom:String,PDifficulte:int,PLangue:int):void
{
addChild(environnementJeu);
addChild(personnageJeu);
}
function test(e:MouseEvent){
pCoordonnees = stage.mouseX;
trace(pCoordonnees);
mouvement(3);
}
}
}
And on mouvement();
package cem
{
public class mouvement {
public function mouvement(blabla) {
trace(blabla);
}
}
}
I searched everywhere I could, and didn't find anything. I have no instances on the stage. Everything is imported on the first frame. I am kind of a beginner (let's say i'm no good at programming), so you can notify at the same time if you something that needs to be corrected. (BTW, the strange words are in french ;D)
Thanks!
The error is due to you trying to cast 3 to mouvement.
I think what you want is something like
function test(e:MouseEvent){
pCoordonnees = stage.mouseX;
trace(pCoordonnees);
var mouve:mouvement = new mouvement(3);
}
Notice that you have to have new in order to create a new instance of a class.
On another note, you should capitilize classes so they stand out better. So I would name the class Mouvement.
You are trying to cast 3 to the class mouvement into the test function:
function test(e:MouseEvent){
pCoordonnees = stage.mouseX;
trace(pCoordonnees);
new mouvement().mouvement(3); // <-- here your error
}
If you have only a function into your class you don't need to create a class but you can put on the function alone:
package cem
{
public function mouvement(blabla):void {
trace(blabla);
}
}
and now you can call the mpuvement function normally into you test function:
function test(e:MouseEvent){
pCoordonnees = stage.mouseX;
trace(pCoordonnees);
mouvement(3);
}