Hi
I am combinig Starling and Nape. It's working but I have a small problem with dragging objects. The following code works. If I run it nape balls will rain down (much similar to Lee Brimelows example). I have extended the code so I can click a ball to drag it. If I click a ball the PivotJoint is actually made, but only as long I'm not moving the cursor. If I move the cursor, the joint is lost. This of course breaks the dragging.
If anyone have a clue why this is happening, please let me know.
Thanks,
Jakob
package main { import com.demonsters.debugger.MonsterDebugger; import flash.display.Stage; import nape.constraint.PivotJoint; import nape.geom.Vec2; import nape.phys.Body; import nape.phys.BodyType; import nape.phys.Material; import nape.shape.Circle; import nape.shape.Polygon; import nape.space.Space; import nape.util.BitmapDebug; import starling.display.DisplayObject; import starling.display.Sprite; import starling.events.Event; import starling.events.TouchEvent; import starling.events.TouchPhase; public class StarlingWorld extends Sprite { private var showDebug:Boolean; private var space:Space; private var debug:BitmapDebug; private var hand:PivotJoint; private var mouseX:Number; private var mouseY:Number; private var mouseDown:Boolean; public function StarlingWorld() { showDebug = true; mouseX = 0; mouseY = 0; mouseDown = false; addEventListener(Event.ADDED_TO_STAGE, thisAddedToStage); } private function thisAddedToStage(e:Event=null):void { MonsterDebugger.initialize(this); MonsterDebugger.trace(this, "StarlingWorld"); removeEventListener(Event.ADDED_TO_STAGE, thisAddedToStage); space = new Space(new Vec2(0, 981)); var floor:Body = new Body(BodyType.STATIC); floor.shapes.add(new Polygon(Polygon.rect(0,550,800,20))); floor.space = space; var wall:Body = new Body(BodyType.STATIC); wall.shapes.add(new Polygon(Polygon.rect(0,0,20,600))); wall.space = space; var wall2:Body = new Body(BodyType.STATIC); wall2.shapes.add(new Polygon(Polygon.rect(800,0,20,600))); wall2.space = space; addBeatle(); hand = new PivotJoint(space.world, null, new Vec2(), new Vec2()); hand.active = false; hand.stiff = false; hand.space = space; if(showDebug) { debug = new BitmapDebug(960, 640, 0x333333); Config.r.addChildAt(debug.display, 0); debug.drawShapeAngleIndicators = false; } stage.addEventListener(TouchEvent.TOUCH, handleStageTouched); addEventListener(Event.ENTER_FRAME, thisEnterFrame); } private function thisEnterFrame(e:Event):void { space.step(1/60); if(showDebug) { debug.clear(); debug.draw(space); debug.flush(); } if(Math.random()<.02) addBeatle(); } private function addBeatle():void { var b:Body = new Body(BodyType.DYNAMIC, new Vec2(Math.random()*800, 0)); b.shapes.add( new Circle(40, null, new Material(.5,1,2,5) ) ); b.space = space; b.graphic = new Beatle(); b.graphicUpdate = updateGraphics; addChild(b.graphic); } private function updateGraphics(b:Body):void { b.graphic.x = b.position.x; b.graphic.y = b.position.y; b.graphic.rotation = b.rotation; } private function handleStageTouched(e:TouchEvent):void { var target:DisplayObject = e.target as DisplayObject; if(e.getTouch(target, TouchPhase.HOVER)) { mouseX = e.getTouch(target, TouchPhase.HOVER).globalX; mouseY = e.getTouch(target, TouchPhase.HOVER).globalY; hand.anchor1.setxy(mouseX,mouseY); } if(e.getTouch(target, TouchPhase.BEGAN)&&!mouseDown) { mouseDown = true; MonsterDebugger.trace(this, "mouseDown"); //var mp:Vec2 = new Vec2(e.getTouch(target, TouchPhase.BEGAN).globalX, e.getTouch(target, TouchPhase.BEGAN).globalY); var mp:Vec2 = new Vec2(mouseX, mouseY); var b:Body = space.bodiesUnderPoint(mp).at(0); hand.body2 = b; hand.anchor2 = b.worldToLocal(mp); hand.active = true; } else if(mouseDown) { mouseDown = false; MonsterDebugger.trace(this, "mouseUp"); hand.active = false; } } } }