gotoAndStop(); will not work right - actionscript-3

I wrote this code for a game im making, Im just learning, There are no compiler errors but when i run the code it fails with no errors. I have a line where i would like to goto a specified frame so the code is Button_Object.gotoAndStop(Local_Frame) but it looks as if the program just skips over it. I have tried to put _root.gotoAndstop(Local_Frame) and stage.gotoAndStop(Local_Frame) but these both give compiler errors the error it gives is
C:\Users\Nathan\Desktop\Matching Game\ClickSolver.as,
Line 34 1120: Access of undefined property _root.
I do see the trace statements. As a side note im trying to access the the main time line, not the objects timeline.
here's the code
package {
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.utils.Timer;
import flash.events.TimerEvent;
public class ClickSolver {
private var Button_Object:MovieClip;
private var Check_Object:MovieClip;
private var Score:Number = 0;
private var Local_Frame:Number = 0;
private var Local_Timer:Timer = new Timer(1000,3);
public function ClickSolver(ButtonObject:MovieClip, CheckObject:MovieClip, Frame:Number) {
Local_Frame = Frame;
Button_Object = ButtonObject;
Check_Object = CheckObject;
Button_Object.buttonMode = true;
trace(Button_Object.name);
trace(Check_Object.name);
Local_Timer.start();
trace(Local_Timer.currentCount);
Button_Object.addEventListener(MouseEvent.CLICK, Object_Button_Clicked);
Button_Object.addEventListener(MouseEvent.MOUSE_OVER,Button_Mouse_Over);
Button_Object.addEventListener(MouseEvent.MOUSE_OUT, Button_Mouse_Out);
Local_Timer.addEventListener(TimerEvent.TIMER_COMPLETE, TimerIsDone);
}
private function TimerIsDone (event:TimerEvent):void{
trace("Timer is done");
Local_Timer.stop();
Local_Timer.reset();
Button_Object.gotoAndStop(Local_Frame);
}
private function Button_Mouse_Out (event:MouseEvent):void{
Button_Object.alpha = 1;
}
private function Button_Mouse_Over (event:MouseEvent):void{
Button_Object.alpha = 0.75;
}
private function Object_Button_Clicked (event:MouseEvent):void{
Score++;
Check_Object.visible = false;
Button_Object.gotoAndStop(Local_Frame);
trace("Score: " + Score);
trace("Frame: " + Local_Frame);
}
}
}

_root isn't supported in AS3.
Try
MovieClip(root).gotoAndStop(local_Frame);
PS. A common naming convention for you code is to name variables using lower camelCase (i.e. start with a lowercase letter) and name classes with upper CamelCase (i.e. start with an uppercase letter).
This will make your code easier to read and allow you to instantly see which items are variables and classes.

Related

as3 #1009 error code provided. "Null Object reference"

hi I'm relatively new to as3 (this year) and I'm getting this error
typer error #1009 cannot access a property or method of a null object
reference. at FoodObject/collisionTest()
i was hoping anyone could help
package {
import flash.events.MouseEvent;
import flash.events.KeyboardEvent;
import flash.events.*
import flash.utils.*
import flash.display.Stage;
public class GameScene1 extends Scene {
//public variables
//character & scenery
public var mainChar: Character;
public var testFood: FoodObject;
//constructor is used to create all necessary objects for this scene and display them
public function GameScene1(gm_: Manager) {
//constructor
super(gm_);
trace("GameScene 1 constructor");
//character
mainChar = new Character;
addChild(mainChar);
mainChar.x = 200;
mainChar.y = 200;
testFood = new FoodObject;
addChild(testFood)
testFood.x = 50
testFood.y = 200
the food object class is here.
package {
import GameScene1
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.TimerEvent;
public class FoodObject extends MovieClip {
public var Game1:GameScene1;
public function FoodObject() {
//constructor code
this.addEventListener(Event.ENTER_FRAME, collisionTest)
}
public function collisionTest(e:Event)
{
if (this.hitTestObject(Game1.mainChar))
{
trace("it works")
}
}
}
}
game manager here:
package {
import flash.display.MovieClip;
public class Manager extends MovieClip {
//stores which scene is currently loaded
public var curScene:Scene=null;
public function Manager() {
//constructor
trace("Manager Construct")
GoToScene("menu");
}
public function GoToScene(name:String)
{
if (curScene) //there was a scene already
{
curScene.OnLeaveScene(); //call its OnLeaveScene function to remove all objects
removeChild(curScene);
}
if(name=="menu") curScene = new MenuScene(this);
if(name=="select") curScene = new SelectScene(this);
if(name=="game1") curScene = new GameScene1(this);
if(name=="game2") curScene = new GameScene2(this);
if(name=="game3") curScene = new GameScene3(this);
if(name=="credit") curScene = new CreditScene(this);
addChild(curScene);
}
}
Your problem is that the concerns of your classes are not separate:
Your Scene knows both the Character and the Food object, you instantiate both classes there, nothing wrong with that.
The problem starts when you are trying to do something in the Food object, that requires knowledge of the character. The thing is: the Food object doesn't know anything about the Character.
You can solve this by simply passing the reference of Character to your Food object. In order to do this, modify the constructor like so:
private var character:Character;
public function FoodObject(character:Character) {
//constructor code
this.addEventListener(Event.ENTER_FRAME, collisionTest)
this.character = character;
}
The usage of said constructor in your Scene changes as follows:
testFood = new FoodObject(mainCharacter);
This way, Food knows the character and can do stuff with it, for example do collision tests:
public function collisionTest(e:Event)
{
if (this.hitTestObject(character)) // ==== this line changed
{
trace("it works")
}
}
However, this raises an important issue: Why should Food know the Character at all?
Sure enough, you want to do the collision test, which requires both objects.
But why do you want to do it in the Food object?
Doing the collision check in Food is cumbersome, because you have to pass a reference to Character in order to do it there.
The much preferred way of doing this is to do the collision check where both objects participating in the check are already known.
In your case, this is the Scene.
Think about how easy it is to do the check in Scene:
testFood.hitTestObject(mainCharacter);
It's that simple, because everything you need is already there.
To recap:
The collision check requires knowledge of 2 objects that you want to
check.
In order to do the check in either one, you have to pass a reference
of the other. (Character to Food as seen above or the other way
round)
It is a lot easier to do the check in some place that already knows
both objects, because no reference have to be passed around.
Your original code failed because Game1 in FoodObject is never assigned a value and therefore remains null.
Invoking methods on null causes the error you experienced.
You forgot to take the instance of GameScene1 class using new keyword.
public var Game1:GameScene1;
public function FoodObject() {
//constructor code
var _manager:Manager = new Manager();
Game1 = new GameScene1(_manager)
this.addEventListener(Event.ENTER_FRAME, collisionTest);
}
public function collisionTest(e:Event):void{
....
}

AS3 - Type not found or is not a compile-time constant

First, before referring me to other posts, I've been looking for a good solution but couldn't figure it out. YES, I have searched, but it didn't help me so I decided to post it here since people here are really smart.
So, I started on making a part for a game in AS3. Since I'm not good with graphics etc. I started first with the AS3 code rather than the flash loader etc. and I tested out connection between AS3 and C# (with sockets).
First frame AS3:
import flash.net.Socket;
import Composer;
var sock:Socket = new Socket();
sock.connect("127.0.0.1", 30000);
sock.addEventListener(IOErrorEvent.IO_ERROR, loaderIOErrorHandler);
sock.addEventListener(Event.CONNECT, connectHandler);
function loaderIOErrorHandler(errorEvent:IOErrorEvent):void{
trace("ioErrorHandler: " + errorEvent);
}
function connectHandler(event:Event):void{
var msg:Composer = new Composer("HELLO_WORLD");
msg.writeDouble(new Date().milliseconds);
sock.writeBytes(msg.toMessageFormat());
}
This is Composer.as (in same folder):
package {
class Composer {
private var _arr:ByteArray;
public function Composer(header:String){
_arr = new ByteArray();
_arr.writeUTF(header);
}
public function writeUTF(utf:String):void{
_arr.writeUTF(utf);
}
public function writeInt(i:int):void{
_arr.writeInt(i);
}
public function writeShort(i:int):void{
_arr.writeShort(i);
}
public function writeFloat(f:Number):void{
_arr.writeFloat(f);
}
public function writeDouble(d:Number):void{
_arr.writeDouble(d);
}
public function writeBoolean(b:Boolean):void{
_arr.writeBoolean(b);
}
public function writeByte(b:int):void{
_arr.writeByte(b);
}
public function toMessageFormat():ByteArray{
var msg:ByteArray = new ByteArray();
msg.writeDouble(_arr.length);
msg.writeBytes(_arr);
return msg;
}
}
}
And I always get the error:
'Type was not found or was not a compile-time constant'
Thanks for helping out.
At the very top of your Composer.as after package {, you have to import any classes you reference, otherwise you'll get this error.
Here's what you're missing
package {
import flash.utils.ByteArray;
class Composer {

AS3 - Access variable in root from class

How to access a variable in the root timeline from within a class? There is a variable called myblocks in the root timeline I need to read the value from.
This is the related class part:
package myclasses
{
public final class Bcoder extends EventDispatcher
{
private function getBlocks():void
{
for (var i:int = _getNumBlocks; i--; ){
// how to get the myblocks value from here?
}}
This is from the root timeline: (ActionScript stands in a keyframe)
import myclasses.Bcoder;
var myblocks:Number=20
I think you might want to look at this: http://www.kirupa.com/forum/showthread.php?349086-AS3-Question-Accessing-a-main-timeline-variable-from-a-class
Furthermore, I'd like to mention that you should use either timeline coding or use a document class, with preference to the latter because this option is the strength of AS3 and will make your code much more structured.
This is complete nonsense and really bad practice. You should avoid this manner of coding!!!
This is really not OOP and make me think about bad AS1 /2 and 3 combined!!!
However this is possible if you have no class defined in Document properties as main Class.
ex :
in a foler "com", the class ObjectOnStage.as :
package com {
import flash.display.Stage;
import flash.display.Sprite;
import flash.events.Event;
public class ObjectOnStage extends Sprite{
public function ObjectOnStage() {
this.addEventListener(Event.ADDED_TO_STAGE,onAddedToStage,false,0,false);
this.addEventListener(Event.ACTIVATED,onActivate,false,0,false);
}
public function onAddedToStage(e:Event):void{
// will output null for both
trace("\n added " + e.target + "\n");
trace(e.target.parent["nonSense"]);
trace(e.target.parent["nonsense"]);
}
public function onActivate(e:Event):void{
// will output the values.
trace("\n added " + e.target + "\n");
trace(e.target.parent["nonSense"]);
trace(e.target.parent["nonsense"]);
}
}
}
On frame 1 of the Timeline :
import com.ObjectOnStage;
var nonSense:int = 1;
var nonsense:String = "This is a nonsense";
var oos:ObjectOnStage = new ObjectOnStage();
this.addChild(oos);
You'd better change the whole script!
Adobe should remove the possibility to write script on the Timeline since the export settings are set to AS3 and the strict mode should be always set to strict mode ON.
Also private constructors will be welcome in order to permit an usage of
MyClass.getInstance();
This will pemit something like:
package com {
public class MyMainObject {
private static var instanceOfMainObject;
private function MyMainObject(args:Vector.<String>){
// or MyMainObject(...args)
trace("new Instance of MyMainObject created with " + args.toString());
}
public static function main(args:Vector.<String>):void{
instanceOfMainObject = MyMainObject.getInstance(args);
trace("arguments.length = " + args.length);
for(var i:int = 0 ; i < args.length ; i++){
trace( i + " = " + args[i]);
}
}
public static function getInstance(args:Vector.<String>):MyMainObject{
var instance:MyMainObject = new MyMainObject(args);
return instance;
}
}
}
Now, this code throws an Error:
1153: A constructor can only be declared public.
Perhaps this will be the case in AS4 ???
If I understand it trough your comment you must pass the DisplayObjectContainer where your variables are declared to the class as argument.
Example :
in MyClass.as
package com {
import flash.display.DisplayObjectContainer;
import flash.events.EventDispatcher;
public class MyClass extends EventDispatcher{
public function MyClass(doc:DisplayObjectContainer) {
trace(doc["nonSense"]);
trace(doc["nonsense"]);
// but this is again not OOP even if you use the "class" KEYWORD.
}
}
}
on the timeline :
var nonSense:int = 1;
var nonsense:String = "This is a nonsense";
var mclss:MyClass = new MyClass(this);
Concerning EventDispatcher you can also read my answer about EventDispatcher here

Trying to delete child instance in array. Lack CS training. Totally stuck

I posted yesterday about how to communicate to one class from another that I wanted to delete an instance of it, and I got the dispatcher working today. However, I think I've painted myself into a corner. Even though the dispatcher is working, I A:feel like it's running through too many functions on the way to actually deleting the object, and B: still can't manage to get it to actually delete. I don't have any formal CS training, so it's one of those situations where my mind is going in circles and I can't "see" what I'm doing wrong. I figure if I post my classes here, at the very least people can have a chuckle at my amateur code, and if I'm lucky, some kind soul will point out what I'm doing wrong. So here goes:
Background.as:
//Background class. Singleton? Sets up/maintains the application.
package pc_mockup {
import flash.display.*;
import flash.events.*;
import flash.geom.*;
public class Background extends flash.display.MovieClip {
private var slate:MovieClip;
private var slateBounds:Rectangle = new Rectangle(100,-260,0,280);
private var _toolbox:MovieClip;
private var _elementArray:Array = new Array();
public function Background() {
//attach movieclips to stage
slate = new mc_slate();
slate.x = 100;
slate.y = 20;
addChild(slate);
_toolbox = new Toolbox();
_toolbox.x = 750;
_toolbox.y = 20;
addChild(_toolbox);
//set draggables
//slate.addEventListener(MouseEvent.MOUSE_DOWN, dragSlate);
//slate.addEventListener(MouseEvent.MOUSE_UP, releaseSlate);
slate.addEventListener(MouseEvent.MOUSE_UP, dropNewElement);
}
private function dragSlate(event:MouseEvent) {
slate.startDrag(false, slateBounds);
}
private function releaseSlate(event:MouseEvent) {
slate.stopDrag();
}
private function dropNewElement(event:MouseEvent) {
var _elementType:String = _toolbox.currentTool;
var _x:Number = event.target.x;
var _y:Number = event.target.y;
var _newElement:MovieClip;
var _latestIndex:Number;
//case switch to choose element based on _elementType
//add new element to stage
_newElement = new PageElement(_elementType, event.localX, event.localY);
_latestIndex = _elementArray.push(_newElement);
_newElement.addEventListener("closeWindow", deleteElement);
slate.addChild(_newElement);
}
private function deleteElement(event:Event) {
trace("trying to remove element.");
slate.event.target.removeChild(_elementArray[0]);
}
}
}
Toolbox.as:
//Toolbox class.
package pc_mockup {
import flash.display.*;
import flash.events.*;
import flash.geom.*;
public class Toolbox extends flash.display.MovieClip {
private var _toolboxback:MovieClip;
private var _tool01:MovieClip;
private var _tool02:MovieClip;
private var _tool03:MovieClip;
private var _tool04:MovieClip;
private var _tool05:MovieClip;
private var _currentTool:String = 'none';
public function Toolbox() {
_toolboxback = new ToolboxBack();
_toolboxback.x = 0;
_toolboxback.y = 0;
_toolboxback.alpha = .5;
addChild(_toolboxback);
_tool01 = new TextTool();
_tool01.x = 10;
_tool01.y = 10;
addChild(_tool01);
//_tool01.addEventListener(MouseEvent.MOUSE_DOWN, dragTool);
_tool01.addEventListener(MouseEvent.MOUSE_UP, switchTool);
_tool02 = new ImageTool();
_tool02.x = 10;
_tool02.y = 54;
addChild(_tool02);
_tool02.addEventListener(MouseEvent.MOUSE_UP, switchTool);
}
private function dragTool(event:MouseEvent) {
event.target.startDrag(false);
}
private function releaseTool(event:MouseEvent) {
event.target.stopDrag();
}
private function switchTool(event:MouseEvent) {
_currentTool = event.target.toolname;
//trace(_currentTool);
}
public function get currentTool():String{
return _currentTool;
}
}
}
Tool.as (any class with "Tool" at the end of it simply extends this class and adds a name)
//Tool class.
package pc_mockup {
import flash.display.*;
import flash.events.*;
import flash.geom.*;
public class Tool extends flash.display.MovieClip {
private var _toolname:String;
public function Tool(toolname) {
_toolname = toolname;
}
public function get toolname():String{
return _toolname;
}
}
}
PageElement.as:
//Page element class.
package pc_mockup {
import flash.display.*;
import flash.events.*;
import flash.geom.*;
import flash.text.*;
public class PageElement extends flash.display.MovieClip {
private var _elementname:String;
private var _elementback:MovieClip;
private var _elementmenu:MovieClip;
private var _title:TextField;
private var _formatter:TextFormat = new TextFormat();
public function PageElement(elementname, x, y) {
_elementname = elementname;
_elementback = new ElementBack();
_elementback.x = x;
_elementback.y = y;
_elementback.alpha = .5;
_elementback.addEventListener(MouseEvent.MOUSE_DOWN, dontBubble);
_elementback.addEventListener(MouseEvent.MOUSE_UP, dontBubble);
_elementmenu = new ElementMenu();
_elementmenu.x = x + _elementback.width - 5;
_elementmenu.y = y - 5;
_elementmenu.addEventListener(MouseEvent.MOUSE_OVER, showElementMenu);
_elementmenu.addEventListener(MouseEvent.MOUSE_OUT, retractElementMenu);
_elementmenu.addEventListener(MouseEvent.MOUSE_DOWN, dragElement);
_elementmenu.addEventListener(MouseEvent.MOUSE_UP, releaseElement);
_formatter.font = "Helvetica";
_formatter.size = 10;
_title = new TextField();
_title.text = elementname;
_title.x = x;
_title.y = y;
_title.textColor = 0xffffff;
_title.setTextFormat(_formatter);
addChild(_title);
addChild(_elementback);
addChild(_elementmenu);
}
public function get elementname():String{
return _elementname;
}
public function set elementTitle(newTitle) {
}
public function showElementMenu(event:MouseEvent) {
_elementmenu.expandMenu();
}
public function retractElementMenu(event:MouseEvent) {
_elementmenu.retractMenu();
}
public function hideElementMenu() {
_elementmenu.alpha = 0;
}
private function dragElement(event:MouseEvent) {
event.target.parent.parent.startDrag(false);
event.stopPropagation();
}
private function releaseElement(event:MouseEvent) {
event.target.parent.parent.stopDrag();
event.stopPropagation();
}
private function dontBubble(event:MouseEvent) {
event.stopPropagation();
}
}
}
DeleteBack.as:
//Element menu back class.
package pc_mockup {
import flash.display.*;
import flash.events.*;
import flash.geom.*;
public class DeleteBack extends flash.display.MovieClip {
public function DeleteBack() {
}
public function closeElement(event:MouseEvent) {
dispatchEvent(new Event("closeWindow", true));
trace("event dispatched.");
}
}
}
ElementMenu.as:
//Element menu class.
package pc_mockup {
import flash.display.*;
import flash.events.*;
import flash.geom.*;
import caurina.transitions.Tweener;
public class ElementMenu extends flash.display.MovieClip {
private var _elementmenuback:MovieClip;
private var _deletebutton:MovieClip;
public function ElementMenu() {
_elementmenuback = new ElementMenuBack();
_elementmenuback.x = 0;
_elementmenuback.y = 0;
_elementmenuback.width = 100;
_elementmenuback.height = 5;
_elementmenuback.alpha = .5;
addChild(_elementmenuback);
_deletebutton = new DeleteBack();
_deletebutton.x = -5;
_deletebutton.y = 10;
_deletebutton.width = 10;
_deletebutton.height = 10;
_deletebutton.alpha = .2;
_deletebutton.visible = false;
addChild(_deletebutton);
_deletebutton.addEventListener(MouseEvent.MOUSE_DOWN, closeElement);
}
public function expandMenu() {
Tweener.addTween(_elementmenuback, {height:30, time:.2, transition:"easeOutBack"});
_deletebutton.visible = true;
}
public function retractMenu() {
Tweener.addTween(_elementmenuback, {height:5, time:.1, transition:"easeInBack"});
_deletebutton.visible = false;
}
public function closeElement(event:MouseEvent) {
//check that the user really wants to close the element before sending the destroy signal
//perform any closing animations
//this.parent.destroy();
_deletebutton.closeElement(event);
}
}
}
That's it for the meaningful classes. Anything else is either an empty class that's only in there to help make the library object accessible to ActionScript, or a trivial extension of something else.
The code puts a new element onto the stage, gives it a cool little dropdown menu that makes it draggable and has a delete button on it, and should link that button to a function that closes the element.
I've got everything but the closing.
General code criticism also very welcome. Like I said, I have no training, I've been figuring this stuff out for myself, and feedback of any kind from people who know what they're doing is valuable.
Thanks!
SS
PS. In response to Daniel's comment, here are the steps the code takes:
The Background class puts everything on the stage and creates the toolbox.
The toolbox creates the tools, which are like Photoshop's tools. You click on them to select an element you want to add to the stage, then you click inside the "slate" to drop a new instance of that object on top of it. The background creates the instance and saves it in an array of all instances created at runtime.
The new element makes its own dropdown menu, which is the draggable portion of the element and holds the delete button. This menu places an eventListener on the delete button.
When the delete button is clicked, the eventListener placed on it by its parent class calls an event dispatcher inside the delete button class itself.
This dispatched event is caught by the background class (I figured the best class to remove the element is the same class that made it, right?) and triggers the actual code to remove the element.
This code, "deleteElement," is where I'm stuck. I have all the instances in an array, but the event has gone through so many intermediary classes, the MouseEvent, and thus, I suspect, the MouseEvent target, has fallen by the wayside. So the only way to know which element to delete is to find its array index. I have no idea how this would work. Any ideas?
let's do this a bit at a time...
slate.event.target.removeChild(_elementArray[0]); in background.as
why are you using slate.event?
you are passing an event object to the function, but looks like you're using a different event's target, which I don't know where it's coming from or why it's not giving you an error.
it should just be event.target, which should give you the PageElement(formerly known as _newElement)
what I don't know also is why you are removing a child from it which is _elementArra[0] - which really is another PageElement and likely itself if you only have one.
so it looks to me that there are a bunch of things that should have thrown errors. What are you using to compile your code? What about debugger? are you using any?
If you look at your previous question, I added some code there about how to get the parent. So I adjusted it a bit
function deleteElement($e:MouseEvent):void{
var parentMC:MovieClip = $e.target.parent;
parentMC.removechild($e.target);
}
however the problem is that you're not passing a MouseEvent but a blank event
dispatchEvent(new Event("closeWindow", true)); in DeleteBack.as
so this will not pass anything under target, and you can't get it. (target is read only, so new Event(etc) will always have a null target. So essentially that's a bit of a lost cause.
you could set an onject in your singleton and pass which mc is to be deleted, and then the deleteElement would just grab that object. The other option is to look into the signals class which will let you do some better/more efficient event handling.
finally (sort of, there's more but for now) I'd say look into using CASAlib, in particular, use CasaMovieClip instead of MovieClip for extending, as it will delete your movie clips better. If you have a lot of event listeners and you don't clear them properly, they'll end up staying in memory even after you delete them.
of course looking into other frameworks like RobotLegs is a good idea too, it gets you into better practices.
GL
Edit ...
frameworks/micro-architectures:
http://www.robotlegs.org/
http://swizframework.org/
http://puremvc.org/
and many more
I think the important thing is to not get stuck on a framework (though I mention the word often). And the best framework is the framework that is best for you, and to me that means offering a good communication backbone for the app and staying out of the way.
My setup for writing code is this:
FlashDevelop with the Flex Compiler. FlashDevelop is for PC only so if you're on Mac you might want to consider other options like flex. FlashDevelop and the Flex compiler(the compiler only) are both free so you can't go wrong, and once you start using it you won't want to go back to coding in Flash - guaranteed!!
debugging:
Trace is the simplest form of debugging, and it can be quite difficult to understand the problem.
You can use the flash debugger by pressing Ctrl-Shift-Enter to compile and run. You will need to set the break points ahead though.
FlashDevelop has a debugger that works just like the Flash and Flex debuggers and I use it quite often.
But my favorite debug tool has to be de monster debugger
it takes a bit to more to implement, and you need to add some code, but it found issues for me that I couldn't get to using the default debugger only. Definitely worth a look.

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