首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在触摸端执行SpriteKit物理体碰撞/接触

在触摸端执行SpriteKit物理体碰撞/接触
EN

Stack Overflow用户
提问于 2013-10-05 20:37:33
回答 1查看 780关注 0票数 0

我有一些物理身体节点。雪碧试剂盒立即调用我希望在我释放触摸时应该调用该方法,以便它可以在释放click时执行操作,而不是立即调用方法,从而给我造成一些操作设置问题。

代码语言:javascript
复制
- (void)didBeginContact:(SKPhysicsContact *)contact
{   NSLog(@"%hhd", _touching);
    if(_touching == NO)
     return;
something here
}

- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
_touching = YES;
 NSLog(@"%hhd", _touching);

}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
_touching = NO;
NSLog(@"%hhd", _touching);
something here
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-10-05 22:43:34

  1. 设置全局变量 BOOL _touching;
  2. 当你触摸/释放(触碰结束并开始)时,你将变量设置为是/否;
  3. 在“第一次接触”中,使用以下内容 如果(_touching ==是){ //当我触摸}时我想要发生的事情,则{ //我不能触摸,所以请这样做}

这里是基本设置--但是我认为这是游戏逻辑的问题所在,也许可以想出一种不同的方法来解决您的问题

代码语言:javascript
复制
@interface XBLMyScene()

@property (strong, nonatomic) SKNode *world;
@property (strong, nonatomic) SKSpriteNode *ball;
@property BOOL touching;
@end

@implementation XBLMyScene

-(id)initWithSize:(CGSize)size {    
if (self = [super initWithSize:size]) {

    self.world = [SKNode node];
    [self addChild:self.world];

    self.backgroundColor = [SKColor colorWithRed:0.15 green:0.15 blue:0.3 alpha:1.0];

    self.physicsBody = [SKPhysicsBody bodyWithEdgeFromPoint:CGPointZero toPoint:CGPointMake(500, 0)];

    self.ball = [SKSpriteNode spriteNodeWithColor:[SKColor redColor] size:CGSizeMake(40, 40)];
    self.ball.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(40, 40)];
    self.ball.position = CGPointMake(200, 300);
    [self.world addChild:self.ball];

    self.touching = NO;

}
return self;
}

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    self.touching = YES;
}

-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

    self.touching = NO;
}

- (void) didSimulatePhysics
{
if (self.touching) {
    NSLog(@"I am touching");
}
else {
    NSLog(@"I am not touching");
}
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19202476

复制
相关文章

相似问题

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