xcode 阻止精灵离开屏幕 cocos2d

发布于 2024-12-01 17:43:42 字数 912 浏览 8 评论 0原文

我使用下面的代码,但这似乎并不能阻止精灵离开屏幕,即使代码构建了。谁能告诉我需要如何更改它,以便当精灵沿 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

貪欢 2024-12-08 17:43:42

修改后,您永远不会将更改后的变量 x 重新分配回您的 vikingsprite。

所以你需要做类似的事情:

if (x < 0 + (spriteWidth/2)) {
    x = 0 + (spriteWidth/2);
} else if (x > screenSize.width - (spriteWidth/2)) {
    x = screenSize.width - (spriteWidth/2);

}
CGPoint vikingPos = cpp(x, vikingsprite.position.y);
[vikingsprite setPosition:vikingPos];

我的语法可能是错误的我不写很多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:

if (x < 0 + (spriteWidth/2)) {
    x = 0 + (spriteWidth/2);
} else if (x > screenSize.width - (spriteWidth/2)) {
    x = screenSize.width - (spriteWidth/2);

}
CGPoint vikingPos = cpp(x, vikingsprite.position.y);
[vikingsprite setPosition:vikingPos];

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

无所的.畏惧 2024-12-08 17:43:42

设置

 boundary.SetAsEdge(b2Vec2(0,0), b2Vec2(winSize.width/PTM_RATIO, 0));

别忘了给 Ray Wenderlich 送一杯啤酒。

Set edge as boundary.

 boundary.SetAsEdge(b2Vec2(0,0), b2Vec2(winSize.width/PTM_RATIO, 0));

Don't forget to send Ray Wenderlich a beer.

半窗疏影 2024-12-08 17:43:42

我认为您忘记重置它的位置?

if (x < 0 + (spriteWidth/2)) {
x = 0 + (spriteWidth/2);
tempNode.position.x = x;
} else if (x > screenSize.width - (spriteWidth/2)) {
x = screenSize.width - (spriteWidth/2);
    tempNode.position.x = x;


}

I think your forgetting to reset it's position?

if (x < 0 + (spriteWidth/2)) {
x = 0 + (spriteWidth/2);
tempNode.position.x = x;
} else if (x > screenSize.width - (spriteWidth/2)) {
x = screenSize.width - (spriteWidth/2);
    tempNode.position.x = x;


}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文