已更新的代码已经按照我的预期工作.请参阅以下更新代码中的dIDSimulatePhysics方法.在我的情况下,我只关心在x轴上向左或向右移动一个字符,x轴上的0是绝对的左边,而在x轴上是一个可配置的值.苹果冒险游戏真的也很有帮助.
// ORIGINAL POST BELOW
我正在与苹果SpriteKit合作,我正在努力实现一个相机,就像我想要的那样.我在代码中所做的是在开始时加载一个精灵字符,两个按钮和一个红色框,该对话框在视图外部.我想要做的是用按钮移动角色,一旦播放器到达屏幕的中间或结尾,相机将重新调整,以发现视图中看不到的内容.所以向右移动应该最终显示一旦玩家到达那里,最初在视图之外的红色框.但是,使用下面的代码,我无法让相机跟随并将坐标调整到主角色.我看过苹果公司的高级场景处理文档以及一些其他的堆栈溢出帖子,但似乎无法正确.如果任何人可以提供一些建议,将不胜感激.
#define cameraEdge 150-(ID)initWithSize:(CGSize)size{ if (self = [super initWithSize:size]) { /* Setup your scene here */ //320 568 self.backgroundcolor = [SKcolor whitecolor]; myWorld = [[SKNode alloc] init]; [self addChild:myWorld]; mainCharacter = [SKSpriteNode spriteNodeWithImagenamed:@"0"]; mainCharacter.physicsBody.dynamic = YES; mainCharacter.name = @"player"; mainCharacter.position = CGPointMake(20,20); CGRect totalScreenSize = CGRectMake(0,800,320); SKSpriteNode *Box = [SKSpriteNode spriteNodeWithcolor:[SKcolor redcolor] size:CGSizeMake(60,60)]; SKSpriteNode *BoxTwo = [SKSpriteNode spriteNodeWithcolor:[SKcolor greencolor] size:CGSizeMake(60,60)]; SKSpriteNode *BoxThree = [SKSpriteNode spriteNodeWithcolor:[SKcolor bluecolor] size:CGSizeMake(60,60)]; BoxThree.position = CGPointMake(40,50); [myWorld addChild:BoxThree]; BoxTwo.position = CGPointMake(1100,50); Box.position = CGPointMake(650,50); [myWorld addChild:Box]; [myWorld addChild:BoxTwo]; self.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromrect:totalScreenSize]; self.physicsWorld.gravity = CGVectorMake(0,-5); mainCharacter.name = @"mainCharacter"; mainCharacter.physicsBody.lineardamPing = 0; mainCharacter.physicsBody.friction = 0; mainCharacter.physicsBody.restitution = 0; mainCharacter.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:mainCharacter.size]; [myWorld addChild:mainCharacter]; [self addChild:[self buildleftbutton]]; [self addChild:[self buildrightbutton]]; } return self;}- (voID)dIDSimulatePhysics{ SKSpriteNode *hero = mainCharacter; if(hero) { CGPoint heroposition = hero.position; CGPoint worldposition = myWorld.position; NSLog(@"%f",heroposition.x); CGfloat xCoordinate = worldposition.x + heroposition.x; if(xCoordinate < cameraEdge && heroposition.x > 0) { worldposition.x = worldposition.x - xCoordinate + cameraEdge; self.worldMovedForUpdate = YES; } else if(xCoordinate > (self.frame.size.wIDth - cameraEdge) && heroposition.x < 2000) { worldposition.x = worldposition.x + (self.frame.size.wIDth - xCoordinate) - cameraEdge; self.worldMovedForUpdate = YES; } myWorld.position = worldposition; }}-(SKSpriteNode *)buildleftbutton{ SKSpriteNode *leftbutton = [SKSpriteNode spriteNodeWithImagenamed:@"left"]; leftbutton.position = CGPointMake(20,20); leftbutton.name = @"leftbutton"; leftbutton.zposition = 1.0; return leftbutton;}-(SKSpriteNode *)buildrightbutton{ SKSpriteNode *leftbutton = [SKSpriteNode spriteNodeWithImagenamed:@"right"]; leftbutton.position = CGPointMake(60,20); leftbutton.name = @"rightbutton"; leftbutton.zposition = 1.0; return leftbutton;}-(voID)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ UItouch *touch = [touches anyObject]; CGPoint location = [touch locationInNode:self]; SKNode *node = [self nodeAtPoint:location]; if([node.name isEqualToString:@"leftbutton"]) { [mainCharacter.physicsBody applyImpulse:CGVectorMake(-120,0)]; } else if([node.name isEqualToString:@"rightbutton"]) { [mainCharacter.physicsBody applyImpulse:CGVectorMake(120,10)]; }}解决方法 如果您希望视图始终以玩家的位置为中心,请根据以下要点修改代码:
1)创建SKNode并将其称为myWorld,worldNode或任何其他名称.
2)添加worldNode [self addChild:worldNode];
3)将所有其他节点添加到worldNode,包括您的播放器.
4)在dIDSimulatePhysics方法中,添加以下代码:
worldNode.position = CGPointMake(-(player.position.x-(self.size.wIDth/2)),-(player.position.y-(self.size.height/2)));
您的观点现在将始终以玩家的位置为中心.
2015年5月更新:
如果您使用的是使用Tiled Map Editor创建的地图,则可以使用免费的SKAToolKit framework.功能包括播放器相机自动跟踪,测试播放器,测试HUD和精灵按钮.
总结以上是内存溢出为你收集整理的ios – 在SpriteKit中移动相机全部内容,希望文章能够帮你解决ios – 在SpriteKit中移动相机所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)