xcode 阻止精灵离开屏幕 cocos2d
我使用下面的代码,但这似乎并不能阻止精灵离开屏幕,即使代码构建了。谁能告诉我需要如何更改它,以便当精灵沿 x 坐标到达屏幕边缘时它会停止。
-(void)applyJoystick:(SneakyJoystick *)aJoystick toNode:(CCNode *)tempNode forTimeDelta:(float)deltaTime
{
CGPoint scaledVelocity = ccpMult(aJoystick.velocity, 1024.0f);
CGPoint newPosition = ccp(tempNode.position.x + scaledVelocity.x * deltaTime, tempNode.position.y);
CGSize screenSize = [CCDirector sharedDirector].winSize;
CGFloat spriteWidth = vikingSprite.contentSize.width;
CGFloat x = tempNode.position.x + scaledVelocity.x * deltaTime;
if (x < 0 + (spriteWidth/2)) {
x = 0 + (spriteWidth/2);
} else if (x > screenSize.width - (spriteWidth/2)) {
x = screenSize.width - (spriteWidth/2);
}
[tempNode setPosition:newPosition];
if (jumpButton.active == YES) {
CCLOG(@"Jump button is pressed.");
}
if (attackButton.active == YES) {
CCLOG(@"Attack button is pressed.");
}
}
谢谢
im using the below code but that doesnt seem to be stopping the sprite from going off screen even though the code builds. can anyone tell me how it needs to be changed so that when the sprite gets to the edge of the screen along the x coordinate it stops.
-(void)applyJoystick:(SneakyJoystick *)aJoystick toNode:(CCNode *)tempNode forTimeDelta:(float)deltaTime
{
CGPoint scaledVelocity = ccpMult(aJoystick.velocity, 1024.0f);
CGPoint newPosition = ccp(tempNode.position.x + scaledVelocity.x * deltaTime, tempNode.position.y);
CGSize screenSize = [CCDirector sharedDirector].winSize;
CGFloat spriteWidth = vikingSprite.contentSize.width;
CGFloat x = tempNode.position.x + scaledVelocity.x * deltaTime;
if (x < 0 + (spriteWidth/2)) {
x = 0 + (spriteWidth/2);
} else if (x > screenSize.width - (spriteWidth/2)) {
x = screenSize.width - (spriteWidth/2);
}
[tempNode setPosition:newPosition];
if (jumpButton.active == YES) {
CCLOG(@"Jump button is pressed.");
}
if (attackButton.active == YES) {
CCLOG(@"Attack button is pressed.");
}
}
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
修改后,您永远不会将更改后的变量 x 重新分配回您的 vikingsprite。
所以你需要做类似的事情:
我的语法可能是错误的我不写很多objective-c,但我使用cocos2d-x,它是cocos2d的C++版本
You never re-assign the changed variable x back to your vikingsprite after you modified it.
So you need to do something like:
My syntax could be wrong I don't write a lot of objective-c, but I use cocos2d-x which is the C++ version of cocos2d
设置
别忘了给 Ray Wenderlich 送一杯啤酒。
Set edge as boundary.
Don't forget to send Ray Wenderlich a beer.
我认为您忘记重置它的位置?
I think your forgetting to reset it's position?