我在继承 SKSpriteNode 时遇到问题。这是我对子类的实现:
@implementation SSSquirrel
- (id) init {
if (self = [super init]) {
// Create Squirrel and physics body
SKSpriteNode *squirrel = [SKSpriteNode spriteNodeWithImageNamed:@"squirrelNormal"];
squirrel.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:squirrel.size.width/2 - 10];
squirrel.physicsBody.dynamic = NO;
squirrel.physicsBody.affectedByGravity = NO;
squirrel.physicsBody.restitution = 0.8;
squirrel.physicsBody.friction = 0.1;
squirrel.zPosition = 1.0;
[self addChild:squirrel];
}
return self;
}
@end
然后我在我的主要游戏场景中创建了一只松鼠:
SSSquirrel *squirrel1 = [[SSSquirrel alloc] init];
squirrel1.position = CGPointMake(offset, self.frame.size.height - offset);
[self addChild:squirrel1];
一切都创建得很好。然而,在游戏过程中,我尝试在触摸开始时“暂停”松鼠物理体,然后在触摸结束时重新启用它(tempSquirrelBody 是场景的 SKPhysicsBody 实例变量)。基本上,我试图给人一种错觉,即当用户触摸屏幕时,松鼠正在向用户移动并越过其他对象。
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
/* Called when a touch begins */
for (UITouch *touch in touches) {
tempSquirrelBody = squirrel1.physicsBody;
squirrel1.physicsBody = nil;
CGPoint newPosition = squirrel1.position;
newPosition.x -= 60;
newPosition.y += 60;
squirrel1.position = newPosition;
squirrel1.texture = [SKTexture textureWithImageNamed:@"squirrelUp"];
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
/* Called when a touch ends */
for (UITouch *touch in touches) {
CGPoint newPosition = squirrel1.position;
newPosition.x += 60;
newPosition.y -= 60;
squirrel1.position = newPosition;
squirrel1.texture = [SKTexture textureWithImageNamed:@"squirrelNormal"];
squirrel1.physicsBody = tempSquirrelBody;
}
}
不幸的是,这不起作用;当 physicsbody 应该设置为 nil 时,物理 body 没有悬浮并且松鼠与其他物体碰撞。当我将代码从我的 SSSquirrel 类移动到游戏场景时,它仍然有效。我做错了什么?
谢谢!
最佳答案
将 collisionBitMask 设置为 0 而不是将整个 physicsBody 设置为 nil 会不会更容易?
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
/* Called when a touch begins */
for (UITouch *touch in touches) {
squirrel1.physicsBody.collisionBitMask = 0;
CGPoint newPosition = squirrel1.position;
newPosition.x -= 60;
newPosition.y += 60;
squirrel1.position = newPosition;
squirrel1.texture = [SKTexture textureWithImageNamed:@"squirrelUp"];
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
/* Called when a touch ends */
for (UITouch *touch in touches) {
CGPoint newPosition = squirrel1.position;
newPosition.x += 60;
newPosition.y -= 60;
squirrel1.position = newPosition;
squirrel1.texture = [SKTexture textureWithImageNamed:@"squirrelNormal"];
squirrel1.physicsBody.collisionBitMask = 0xFFFFFFFF;
}
}
关于ios - 子类化 SKSpriteNode 时物理体问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20510436/
我有一个模型:classItem项目有一个属性“商店”基于存储的值,我希望Item对象对特定方法具有不同的行为。Rails中是否有针对此的通用设计模式?如果方法中没有大的if-else语句,这是如何干净利落地完成的? 最佳答案 通常通过Single-TableInheritance. 关于ruby-on-rails-Rails-子类化模型的设计模式是什么?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.co
我想为Heroku构建一个Rails3应用程序。他们使用Postgres作为他们的数据库,所以我通过MacPorts安装了postgres9.0。现在我需要一个postgresgem并且共识是出于性能原因你想要pggem。但是我对我得到的错误感到非常困惑当我尝试在rvm下通过geminstall安装pg时。我已经非常明确地指定了所有postgres目录的位置可以找到但仍然无法完成安装:$envARCHFLAGS='-archx86_64'geminstallpg--\--with-pg-config=/opt/local/var/db/postgresql90/defaultdb/po
尝试通过RVM将RubyGems升级到版本1.8.10并出现此错误:$rvmrubygemslatestRemovingoldRubygemsfiles...Installingrubygems-1.8.10forruby-1.9.2-p180...ERROR:Errorrunning'GEM_PATH="/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/ruby-1.9.2-p180@global:/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/rub
我的最终目标是安装当前版本的RubyonRails。我在OSXMountainLion上运行。到目前为止,这是我的过程:已安装的RVM$\curl-Lhttps://get.rvm.io|bash-sstable检查已知(我假设已批准)安装$rvmlistknown我看到当前的稳定版本可用[ruby-]2.0.0[-p247]输入命令安装$rvminstall2.0.0-p247注意:我也试过这些安装命令$rvminstallruby-2.0.0-p247$rvminstallruby=2.0.0-p247我很快就无处可去了。结果:$rvminstall2.0.0-p247Search
由于fast-stemmer的问题,我很难安装我想要的任何rubygem。我把我得到的错误放在下面。Buildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingfast-stemmer:ERROR:Failedtobuildgemnativeextension./System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/rubyextconf.rbcreatingMakefilemake"DESTDIR="cleanmake"DESTDIR=
这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下
当我尝试安装Ruby时遇到此错误。我试过查看this和this但无济于事➜~brewinstallrubyWarning:YouareusingOSX10.12.Wedonotprovidesupportforthispre-releaseversion.Youmayencounterbuildfailuresorotherbreakages.Pleasecreatepull-requestsinsteadoffilingissues.==>Installingdependenciesforruby:readline,libyaml,makedepend==>Installingrub
下面例子中的Nested和Child有什么区别?是否只是同一事物的不同语法?classParentclassNested...endendclassChild 最佳答案 不,它们是不同的。嵌套:Computer之外的“Processor”类只能作为Computer::Processor访问。嵌套为内部类(namespace)提供上下文。对于ruby解释器Computer和Computer::Processor只是两个独立的类。classComputerclassProcessor#Tocreateanobjectforthisc
我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的
我正在尝试使用boilerpipe来自JRuby。我看过guide从JRuby调用Java,并成功地将它与另一个Java包一起使用,但无法弄清楚为什么同样的东西不能用于boilerpipe。我正在尝试基本上从JRuby中执行与此Java等效的操作:URLurl=newURL("http://www.example.com/some-location/index.html");Stringtext=ArticleExtractor.INSTANCE.getText(url);在JRuby中试过这个:require'java'url=java.net.URL.new("http://www