iOS SpriteKit-光线(线)从边界反射
我们有一条线(光线),它移动到边界,并在到达边界时反射。这些图像显示了运动和反射的动态iOS SpriteKit-光线(线)从边界反射,ios,iphone,sprite-kit,game-physics,Ios,Iphone,Sprite Kit,Game Physics,我们有一条线(光线),它移动到边界,并在到达边界时反射。这些图像显示了运动和反射的动态 我想在SpriteKit中实现它(最好是obj-c),但不知道从哪一点开始。我找到了如何实现它。希望对其他人有用 #import "GameScene.h" static const float GUIDE_MASS = .0015; static const int SEGMENTS_COUNT = 10; static const int SEGMENT_LENGTH = 5; @in
我想在SpriteKit中实现它(最好是obj-c),但不知道从哪一点开始。我找到了如何实现它。希望对其他人有用
#import "GameScene.h"
static const float GUIDE_MASS = .0015;
static const int SEGMENTS_COUNT = 10;
static const int SEGMENT_LENGTH = 5;
@interface GameScene(private)
-(void) createGuidesAndShot;
@end
@implementation GameScene
SKShapeNode *shot;
NSMutableArray* shotSegments;
-(void)didMoveToView:(SKView *)view {
[self setBackgroundColor:[SKColor whiteColor]];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
/* Called when a touch begins */
}
-(void)update:(CFTimeInterval)currentTime {
CGMutablePathRef pathToDraw = CGPathCreateMutable();
if (shotSegments!=nil) {
bool isFirst = YES;
for (SKNode* segment in shotSegments) {
if(isFirst){
CGPathMoveToPoint(pathToDraw, NULL,
segment.position.x,
segment.position.y);
isFirst =NO;
} else {
CGPathAddLineToPoint(pathToDraw, NULL,
segment.position.x,
segment.position.y);
}
}
shot.path = pathToDraw;
}
}
-(void)didBeginContact:(SKPhysicsContact *)contact {
uint32_t collision = (contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask);
if (collision == (shotCategory|screenBoundsCategory)){
}
}
-(id)initWithSize:(CGSize)size {
if (self = [super initWithSize:size]) {
shotSegments = [NSMutableArray new];
self.physicsWorld.gravity = CGVectorMake(0.0f, 0.0f);
SKPhysicsBody* borderBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame];
self.physicsBody = borderBody;
self.physicsBody.friction = 0.0f;
self.physicsBody.categoryBitMask = screenBoundsCategory;
self.physicsBody.contactTestBitMask = shotCategory;
self.physicsWorld.contactDelegate = self;
[self createGuidesAndShot];
}
return self;
}
-(void) createGuidesAndShot{
for (int i = 0; i<SEGMENTS_COUNT*SEGMENT_LENGTH; i+=SEGMENT_LENGTH) {
SKShapeNode* guide = [SKShapeNode shapeNodeWithCircleOfRadius:1];
guide.position = CGPointMake(self.frame.size.width/2+i,
self.frame.size.height/2-i);
// guide.fillColor = [SKColor blackColor];
guide.name = [NSString stringWithFormat:@"guide%i", i];
[self addChild:guide];
[shotSegments addObject:guide];
guide.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:guide.frame.size.width/2];
guide.physicsBody.friction = 0.0f;
guide.physicsBody.restitution = 1.0f;
guide.physicsBody.linearDamping = 0.0f;
guide.physicsBody.allowsRotation = NO;
guide.physicsBody.categoryBitMask = shotCategory;
guide.physicsBody.contactTestBitMask = screenBoundsCategory;
guide.physicsBody.collisionBitMask = screenBoundsCategory;
guide.physicsBody.mass = GUIDE_MASS;
[guide.physicsBody applyImpulse:CGVectorMake(0.1f, -0.1f)];
}
shot = [SKShapeNode node];
[shot setStrokeColor:[UIColor redColor]];
[self addChild:shot];
}
@end
#导入“GameScene.h”
静态常数浮动导轨_质量=0.0015;
静态常数int段\ u计数=10;
静态常数int段长度=5;
@界面游戏场景(私人)
-(无效)创建向导和快照;
@结束
@实现游戏场景
SKShapeNode*镜头;
NSMutableArray*快照段;
-(void)didMoveToView:(SKView*)视图{
[self-setBackgroundColor:[SKColor whiteColor]];
}
-(无效)触摸开始:(NSSet*)触摸事件:(UIEvent*)事件{
/*当触摸开始时调用*/
}
-(无效)更新:(CFTimeInterval)currentTime{
CGMutablePathRef pathToDraw=CGPathCreateMutable();
if(shot段!=nil){
bool isFirst=是;
用于(SKNode*段在shotSegments中){
如果(isFirst){
CGPathMoveToPoint(pathToDraw,NULL,
段.position.x,
段(位置y);
isFirst=否;
}否则{
CGPathAddLineToPoint(pathToDraw,NULL,
段.position.x,
段(位置y);
}
}
shot.path=pathToDraw;
}
}
-(无效)didBeginContact:(SKPhysicsContact*)联系人{
uint32_t collision=(contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask);
如果(碰撞==(快照类别|屏幕边界散点){
}
}
-(id)initWithSize:(CGSize)大小{
if(self=[super initWithSize:size]){
shotSegments=[NSMutableArray new];
self.physicsWorld.gravity=CGVectorMake(0.0f,0.0f);
SKPhysicsBody*borderBody=[SKPhysicsBody-withedgeloopfromrect:self.frame];
self.physicsBody=边界体;
self.physicsBody.摩擦力=0.0f;
self.physicsBody.categoryBitMask=screenbundscategory;
self.physicsBody.contactTestBitMask=shotCategory;
self.physicsWorld.contactDelegate=self;
[自创指南和快照];
}
回归自我;
}
-(无效)创建向导和快照{
对于(int i=0;i您不清楚哪些点?