gpt4 book ai didi

iphone - 在 cocos2d 中绑定(bind) Sprite

转载 作者:太空狗 更新时间:2023-10-30 03:38:18 25 4
gpt4 key购买 nike

我正在制作用于发射物体的佳能。佳能的背面附有柱塞。柱塞作用于设定的速度和角度。佳能旋转 0-90 度,柱塞前后移动以调节速度。当我通过触摸旋转佳能时,它的工作正常。 当柱塞被移动的触摸拉回并且它旋转时意味着柱塞在标准之外。

如何控制这个:-

我的触摸时柱塞和佳能旋转代码已移动。 (para3 是佳能,para6 是我的柱塞):-

    CGPoint touchLocation = [self convertTouchToNodeSpace:touch];
CGPoint oldTouchLocation = [touch previousLocationInView:touch.view];
oldTouchLocation = [[CCDirector sharedDirector] convertToGL:oldTouchLocation];
oldTouchLocation = [self convertToNodeSpace:oldTouchLocation];
if (CGRectContainsPoint(CGRectMake(para6.position.x-para6.contentSize.width/2, para6.position.y-para6.contentSize.height/2, para6.contentSize.width, para6.contentSize.height), touchLocation) && (touchLocation.y-oldTouchLocation.y == 0))
{
CGPoint diff = ccpSub(touchLocation, oldTouchLocation);
CGPoint currentpos = [para6 position];
NSLog(@"%d",currentpos);
CGPoint destination = ccpAdd(currentpos, diff);
if (destination.x < 90 && destination.x >70)
{
[para6 setPosition:destination];
speed = (70 + (90-destination.x))*3.5 ;
}

}
if(CGRectIntersectsRect((CGRectMake(para6.position.x-para6.contentSize.width/8, (para6.position.y+30)-para6.contentSize.height/10, para6.contentSize.width, para6.contentSize.height/10)),(CGRectMake(para3.position.x-para3.contentSize.width/2, para3.position.y-para3.contentSize.height/2, para3.contentSize.width, para3.contentSize.height))))

{

[para3 runAction:[CCSequence actions:
[CCRotateTo actionWithDuration:rotateDuration angle:rotateDiff],
nil]];
CGFloat plungrot = (rotateDiff);
CCRotateTo *rot = [CCRotateTo actionWithDuration:rotateDuration angle:plungrot];
[para6 runAction:rot];
}
}

最佳答案

使用 [CCMoveTo actionWithDuration: position:] 方法如何?

通过这种方法,您可以通过“actionWithDuration”参数轻松控制速度,该参数采用以秒为单位的整数值时间,而方向可以通过“position”参数进行调整,该参数采用 ccp(x,y) 作为您希望柱塞移动到的点。

你可以像这样使用它......

    CCSprite *plunger = [[CCSprite alloc] initWithFile:@"plunger.png"];
plunger.position = ccp(240,240);
[self addChild:plunger z:10];
[plunger release];

id = [CCMoveTo actionWithDuration:3 position:ccp(300,240)];

给出的值是我选择的。您可以根据自己的意愿使用它们。

希望对你有帮助....

关于iphone - 在 cocos2d 中绑定(bind) Sprite ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4632925/

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