首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Nape物理ShapeDebug似乎已扩展

Nape物理ShapeDebug似乎已扩展
EN

Stack Overflow用户
提问于 2013-11-18 00:36:49
回答 2查看 566关注 0票数 0

我有一个奇怪的问题,一个简单的项链演示...这是我的代码

代码语言:javascript
复制
package com.secretpackage {

   import flash.display.Sprite;
   import flash.events.Event;
   import flash.text.TextField;
   import nape.phys.Body;
   import nape.shape.Circle;
   import nape.space.Space;
   import nape.util.ShapeDebug;
   import nape.geom.Vec2;
   import nape.phys.BodyType;
   import nape.phys.Material;

   public class Main extends Sprite {

    // -------
    private var world_db:Space=new Space(new Vec2(0,1000));
    private var debug:ShapeDebug;
    private var napeDebugSprite:Sprite = new Sprite();
    private var sphere:Body;
    private var labels:TextField;
    // -------

    public function Main():void 
    {
        if (stage) init();
        else addEventListener(Event.ADDED_TO_STAGE, init);
    }

    private function init(e:Event = null):void 
    {
        removeEventListener(Event.ADDED_TO_STAGE, init);
        // entry point


        debug  = new ShapeDebug(stage.stageWidth, stage.stageHeight, stage.color);

        labels = new TextField();

        var posX:int = stage.stageWidth/4;
        var posY:int = stage.stageHeight/4;
        var r:int = 10;
        sphere= new Body(BodyType.KINEMATIC, new Vec2(posX, posY));
        sphere.shapes.add(new Circle(r, new Vec2(posX, posY), Material.rubber()));
        sphere.space = world_db;
        labels.x = 0;
        labels.y = 0;
        addChild(labels);

        var tc:test_circle = new test_circle(); 
        addChild(tc);
        tc.x = stage.stageWidth / 2;
        tc.y = stage.stageHeight / 2;

        addChild(napeDebugSprite);
        debug.drawConstraints=true;
        napeDebugSprite.addChild(debug.display);

        addEventListener(Event.ENTER_FRAME, update);
    }

    private function update(e:Event):void {
        world_db.step(1/stage.frameRate, 10, 10);
        debug.clear();
        debug.draw(world_db);
        debug.flush();
        labels.text = sphere.position.x + " - " + sphere.position.y + 
                        "\n" + stage.stageWidth + " - " + stage.stageHeight + 
                        "\n" + napeDebugSprite.width + " - " + napeDebugSprite.height + 
                        "\n" + debug.display.width + " - " + debug.display.height;
    }
}

    }

请注意:

代码语言:javascript
复制
- sphere is a Circle with radius=10 placed at StageWidth/4 and StageHeight/4;
- test_circle is a Movieclip of a circle with radius=10 placed at StageWidth/2 and StageHeight/2;

当我编译这个脚本时,我得到... http://i.imgur.com/MAYF3j9.png?1,两个圆居中,球体半径加倍。

我做错了什么吗?

谢谢。

EN

回答 2

Stack Overflow用户

发布于 2013-12-15 01:54:55

您将Body设置为具有位置(stageWidth/4,stageHeight/4),然后添加了一个Circle形状,其局部偏移也是(stageWidth/4,stageHeight/4),因此当body不旋转时,圆将最终位于(stageWidth/2,stageHeight/2)。

至于图形大小的差异,我只能假设您的图形实际上不是“半径10”,而是“宽/高10”,它是半径10的圆(其宽/高为20)的宽度/高度的一半。

票数 1
EN

Stack Overflow用户

发布于 2013-11-18 00:56:05

一个临时的解决方案是扩展debug:ShapeDebug,这样代码就变得...

代码语言:javascript
复制
    ....
    addChild(napeDebugSprite);
    debug.drawConstraints=true;
    //Scaling
    debug.display.scaleY = 0.5;
    debug.display.scaleX = 0.5;

    napeDebugSprite.addChild(debug.display);
    ....

这解决了问题,但这只是一种变通方法。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20033257

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档