views:

646

answers:

1

I'm playing with a Physics Engine for AS3 and the code below is basically the hello world example. However, I made a slight change by declaring the property "ball" as a WheelParticle at the beginning of the class definition. (forgive my terminology if it's wrong). Before, it was declared inside the constructor as

var ball:WheelParticle = new WheelParticle(...);

and that worked fine. But now, trying it my way I get the error

Implicit Coercion of a value of type org.cove.ape.WheelParticle to an unrelated type Class | ball:WheelParticle = new... etc etc

"illegal assignment to class WheelParticle"

So I'm thinking my declaration public var ball:WheelParticle should be something else. But what?

Anyway, here's the code. It's quite short. I would refer to the documentation, but there is none. None that I can find anyway.

package{

    import org.cove.ape.*;
    import flash.events.*;
    import flash.display.Sprite; 

    public class Bounce extends Sprite {

      public var ball:WheelParticle;

    public function Bounce() {

     stage.focus = this;
     stage.frameRate = 100;
     addEventListener(Event.ENTER_FRAME, run);

     stage.addEventListener(KeyboardEvent.KEY_DOWN, moveLeft);

     APEngine.init(0.3);
     APEngine.container = this;
     APEngine.addMasslessForce(new Vector(0,2));

     var defaultGroup:Group = new Group();
     defaultGroup.collideInternal = true;

     var ball:WheelParticle = new WheelParticle(250,10,40, false, 1, 0.7, 0.1);
     defaultGroup.addParticle(ball);

     var rp:RectangleParticle = new RectangleParticle(250,300,300,50,0,true);
     defaultGroup.addParticle(rp);


     APEngine.addGroup(defaultGroup);
      }

      private function moveLeft(e:Event):void{
       ball.speed += 1;
    }

      private function run(evt:Event):void {
         APEngine.step();
         APEngine.paint();
      }
   }

}

+2  A: 

Import the WheelParticle class (along with every other class you need) explicitly as:

package {
import flash.display.Sprite;
import flash.events.Event;
import flash.events.KeyboardEvent;

import org.cove.ape.APEngine;
import org.cove.ape.Group;
import org.cove.ape.RectangleParticle;
import org.cove.ape.Vector;
import org.cove.ape.WheelParticle;

    public class Bounce extends Sprite {

That missing import is the most likely cause of your error. Also, note that your sample code has a small glitch.

 public class Bounce extends Sprite {

     public var ball:WheelParticle;

declares a member variable for class Bounce of type WheelParticle called ball.

var ball:WheelParticle = new WheelParticle(250,10,40, false, 1, 0.7, 0.1);

Creates another variable with same type and same name but local to the constructor of Bounce. And this one shadows the former. If you want to use the member variable use:

ball = new WheelParticle(250,10,40, false, 1, 0.7, 0.1);
dirkgently