Domanda

Im new to actionscript and I have have been mucking around trying to create a sidescrolling game. All was going well until I realized I need a game over feature. The problem is I have many movieclip objects with their own instance names that I access from my document class.

However, now I have a new document class that initiates my old document class to start the game, and also initiates the game over screen, so i can reset the game. Except now I get the error access of undefined property (fill in the blank) for all 50 of my instance objects.

So my question is how can I get flash to recognize and allow my non document class, class to manipulate these Movie Clip instances.

I created a smallscale example to illustrate what im trying to do.

package  {
import flash.display.MovieClip;
import flash.events.Event;
import flash.display.Stage;
public class Test extends MovieClip {

    public static var gstage:Stage;

    public function Test() {
        // constructor code
        var playScreen:Go = new Go();
        addChild( playScreen );
    }

}

}

The above is my Document class "moveme is the name of the instance

The other class which would be my old Document class is

package  {
import flash.display.MovieClip;
import flash.display.Stage;
import flash.events.Event;
import Test;

public class Go extends MovieClip {
    public var test:Test;

    public function Go() 
    {
        if (stage)
        {
            init();
        }
        else
        {
        addEventListener(Event.ADDED_TO_STAGE, init);
        }
    }
    private function init(e:Event = null):void 
    {       
           if (e) removeEventListener(Event.ADDED_TO_STAGE, init);
            trace(stage.width);
            addEventListener(Event.ENTER_FRAME, loop);

    }
    public function loop(e:Event, Test):void
    {
        Test.moveme.scaleX++
                    Test.moveme.x++

    }

}

}

The error I get with this one is ArgumentError: Error #1063: Argument count mismatch on Go/loop(). Expected 2, got 1.

or if I remove the Test in public function loop(e:Event, Test):void I get the same error as before. Ive tried many things already I can get it to trace the stage width I just cant seem to control the instances.

Any help would be appreciated!

È stato utile?

Soluzione

This is wrong for following reasons:

 public function loop(e:Event, Test):void
    {
        Test.moveme.scaleX++
        Test.moveme.x++

    }
  • Test - is a class name, if you want to make it as a parameter you should write something like public function loop(e:Event, testObj:Test):void
  • The event listener won't pass the testObj. So if you want to use it both with an event listener as an event handler AND be able to call it just by yourself, you should change to public function loop(e:Event = null, testObje:Test = null):void
    Then the event listener won't cause the error. Calling the function by itself could be done like this: loop(null, yourInstanceOfTest)

Also, the body of the function is trying to call methods, variables of the class, not the instance, it should be:

 {
     test.moveme.scaleX++
     test.moveme.x++

 }

In your case, because test is an instance of the Class, Test is a Class.
But then there is no reason to pass Test as a parameter, just remove it and keep only the e:Event part. If you do want to use it, then use the passed parameter instance, like this:

{
         // provided that use passed parameter type of Test as a testObj, like in my example
         testObj.moveme.scaleX++
         testObj.moveme.x++

}

Altri suggerimenti

When the event Event.ENTER_FRAME triggers and calls loop, it only passes one argument, the event itself, to the function "loop". Function "loop" however expects 2 arguments, which are "e:Event" and "Test" of unknown type. You can either remove the second argument from the function since you dont use it in the function or if you plan to call the function from somewhere else later and pass a second argument have set it to null so that the Event.ENTER_FRAME can properly call it:

Either:

public function loop(e:Event):void

Or:

public function loop(e:Event, test : Test = null):void
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top