Draw in color the shape of your SKPhysicsBody

天大地大妈咪最大 提交于 2019-12-10 04:13:45

问题


Creating a game with SpriteKit and its actually working out very well. I am using physics and having the ability to see actually where my bodies are because I might have some alpha inside of my sprites will really be helpful. This will also be helpful for creating more precise bodies.

In the documentation for SpriteKit they talk about a debugOverlay but the only example they give is to draw the direction of gravity. Here is the link: https://developer.apple.com/library/ios/documentation/GraphicsAnimation/Conceptual/SpriteKit_PG/Actions/Actions.html

Scroll to area of debugging overlay.

Has anybody written some code that can easily access the body of all objects in a scene and draw the exact size of them and place them where they should be in the correlation? I'm very surprised apple did not just add some type of DrawGizmos property to the physics body.

Thanks in advance.


回答1:


This is from RW tutorial book.

- (void)attachDebugRectWithSize:(CGSize)s {
    CGPathRef bodyPath = CGPathCreateWithRect( CGRectMake(-s.width/2, -s.height/2, s.width,   s.height),nil);
    [self attachDebugFrameFromPath:bodyPath];
    CGPathRelease(bodyPath); 
 }

 - (void)attachDebugFrameFromPath:(CGPathRef)bodyPath {
     //if (kDebugDraw==NO) return;
     SKShapeNode *shape = [SKShapeNode node];
     shape.path = bodyPath;
     shape.strokeColor = [SKColor colorWithRed:1.0 green:0 blue:0 alpha:0.5];
     shape.lineWidth = 1.0;
     [self addChild:shape]; 
  }

To use this on a node,

 [mySpriteToDebug attachDebugRectWithSize:contactSize];



回答2:


I'm sure you've long since answered your question, but as of iOS 7.1 it is now possible to use:

skView.showsPhysics = YES;

Which works much better than the YMCPhysicsDebugger. YMCPhysicsDebugger is also no longer supported.




回答3:


You can use a library by ymc-thzi.
https://github.com/ymc-thzi/PhysicsDebugger

Usage:

import "YMCPhysicsDebugger.h"

- (void)createSceneContents {

  [YMCPhysicsDebugger init];

  // Create your nodes 

  [self drawPhysicsBodies];
}


来源:https://stackoverflow.com/questions/20084968/draw-in-color-the-shape-of-your-skphysicsbody

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!