Lonergan6275
Lonergan6275

Reputation: 2038

nape physics attaching a movie clip to multiple nape bodies. Implicit coercion error

i am trying to attach a movie clip to multiple nape bodies but am getting the following error.

Line 118    1067: 
Implicit coercion of a value of type flash.display:MovieClip 
to an unrelated type nape.shape:Shape.

and here is the block of code:

for (var i:int = 0; i < 10; i++)
{
     var brick:Body= new Body(BodyType.DYNAMIC);
     var brickShape:Polygon = new Polygon(Polygon.box(10,30));
     brick.position.setxy(500, ((h ) - 32 * (i + 0.5)));
     var brickMovieClip:MovieClip = new Brick();
     brickMovieClip.width = 10;
     brickMovieClip.height = 30;
     addChild(brickMovieClip);
     brick.shapes.add(brickMovieClip);              
     brick.space = space;
    brick.shapes.at(0).material.elasticity = .1;            
}

if you have any idea how to fix this your help would be greatly appreciated.

Upvotes: 1

Views: 1412

Answers (1)

deltaluca
deltaluca

Reputation: 1652

the shapes property of Body is a list of nape.shape.Shape's, not MovieClip's, you should be adding the brickShape to the body's shapes list, not a Movieclip.

Nape itself has nothing to do with graphics, and its up to you each frame, to move the grapihc associated with a body to the correct position/rotation based on the physics object state.

What you can do, is store the movieclip in the userData field of the body so that you can access it easily later if necessary like:

body.userData.graphic = brickMovieClip;

then each frame you can update the graphic like:

var mc:MovieClip = body.userData.graphic;
mc.x = body.position.x;
mc.y = body.position.y;
mc.rotation = body.rotation * 180 / Math.PI;

Upvotes: 3

Related Questions