gpt4 book ai didi

iphone - 如何实现像把球扔进桶里的效果?

转载 作者:行者123 更新时间:2023-12-03 20:15:38 26 4
gpt4 key购买 nike

我是 Cocos2d 的新手,因此不了解大多数类和函数。在我的应用程序中,我想实现一个模块,例如触摸并沿桶的方向拖动球将传递球并将球放入桶中。我能够朝该方向传球,但随后它就会按照坐标(x.y)离开屏幕。

    - (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{
// Choose one of the touches to work with
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:[touch view]];
location = [[CCDirector sharedDirector] convertToGL:location];

// Determine offset of location to projectile
int offX = location.x - ball.position.x;
int offY = location.y - ball.position.y;

// Bail out if we are shooting down or backwards
if (offX <= 0)
return;

// Determine where we wish to shoot the projectile to
int realX = winSize.width + (ball.contentSize.height/2);
float ratio = (float) offY / (float) offX;
int realY = (realX * ratio) + ball.position.y;
CGPoint realDest = ccp(realX, realY);

if(realX>=320)
realX = 320;
if(realY>=480)
realY = 480;
CGPoint realDest = ccp(realX, realY);

int good = goodBarrel.position.x;
int bad = badBarrel.position.x;

int destY = realDest.x;
if(destY<=good)
destY = good;

if(destY>=bad)
destY = bad;

realDest.x = destY+10;

// Determine the length of how far we're shooting
int offRealX = realX - ball.position.x;
int offRealY = realY - ball.position.y;
float length = sqrtf((offRealX*offRealX)+(offRealY*offRealY));
float velocity = 480/1; // 480pixels/1sec
float realMoveDuration = length/velocity;

// Move projectile to actual endpoint
[ball runAction:[CCSequence actions:
[CCMoveTo actionWithDuration:realMoveDuration position:realDest],
[CCCallFuncN actionWithTarget:self selector:@selector(spriteMoveFinished:)],
nil]];
}

我需要做类似的事情,它将了解桶的位置并找到桶的顶部,并将球放在桶的中心,并且在移动球时,球的大小会减小到产生将球扔到远处的效果。 GoodBarrel 和 BadBarrel 是我放在球对面的两个桶,以便将球放在同一个地方。有什么功能或方法我可以使用吗?

最佳答案

紧接着 [ball runAction: .... ];行,您可以同时对同一对象运行另一个操作。添加一个

[ball runAction:[CCScaleTo actionWithDuration:realMoveDuration scale:0.4f];

这两个 Action 将同时完成,提供您想要创建的距离的“幻觉”。

关于iphone - 如何实现像把球扔进桶里的效果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8665048/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com