我只是想知道如何做到这一点并希望有人做了类似的事情。当手指留在屏幕上时,我想要一个精灵来缩放。我可以看到任何提示,一般指导方针或方法。
答案 0 :(得分:3)
定义你想要执行的动作,在这种情况下......一个ScaleBy(你想要它相对于当前的比例,而不是一个确定的比例)。收到触摸后,重复执行此操作直到触摸结束,然后停止操作。
如果你想实现一个上限以便项目不会超过某一点,那么你的ScaleBy将成为一个ScaleTo,其上限设置为目标比例。然而,你必须玩弄持续时间以获得你所寻找的感觉。太快,太慢等......这些都取决于你。
- (void)ccTouchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
UITouch* touch = [touches anyObject];
CGPoint location = [[[Director sharedDirector] convertCoordinate: touch.location];
CGRect particularSpriteRect = CGMakeRect(particularSprite.position.x, particularSprite.position.y, particularSprite.contentSize.width, particularSprite.contentSize.height);
if(CGRectContainsPoint(particularSpriteRect, location)) {
// scale by 1.25 every 0.25 second while finger touching
id a = [RepeatForever actionWithAction: [ScaleBy actionWithDuration: 0.25 scale: 1.25];
[a setTag: YOUR_TAG];
[particularSprite runAction: a];
return kEventHandled;
}
}
- (void)ccTouchesEnded:(NSSet*)touches withEvent:(UIEvent*)event {
// code to determine correct finger/touch releases omitted
[particularSprite stopActionByTag: YOUR_TAG];
}
答案 1 :(得分:1)
当我找到你的时候你想要缩放精灵,而手指保持在同一个位置而不移动是正确的吗?
我建议使用Layer
以下方法:
- (BOOL)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
- (BOOL)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
- (BOOL)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
您可以在ccTouchesBegan
中安排基于间隔的操作/方法以增加精灵的比例(例如每秒增加0.1),并在ccTouchesEnded
中取消该操作并将比例设置回原始值(例如1.0)。
对于调度,请查看该方法(可在任何扩展CocosNode
的类中找到):
- (void)schedule:(SEL)s;
答案 2 :(得分:0)
嗯...... sprite类似乎有一个scale方法,可以让你设置它的比例,作为一个浮点值,其中1.0是默认(“原生”)比例。
你不是很清楚你想要精灵如何缩放,如果它应该缩放以“跟随”触摸,即让用户通过拖动重新调整精灵的大小,或者如果只是去(为示例)更改其比例以通知用户已注册触摸。你需要弄清楚这些事情,并计算出你想要的规模。
答案 3 :(得分:0)
我刚刚使用cocos2D和SpaceManager实现了一次放大/缩小。已经接受的答案看起来很好,但我想展示如果你已经在使用SpaceManager,你可以如何做到这一点。
注意:此片段一下子放大和缩小。可以查看已经接受的答案,看看如何将其修改为仅在发布时进行缩小。
我就这样做了:
#pragma mark touch
- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
NSLog(@"Touched began");
CGPoint position = [touch locationInView: [touch view]];
CGPoint positionLocal =[[CCDirector sharedDirector] convertToGL: position];
cpShape *touchedShape = [smgr getShapeAt:positionLocal];
if (touchedShape != nil) {
NSLog(@"something was touched");
cpCCSprite *cpCCSOwner = (cpCCSprite *) touchedShape->data;
// Let's do 'pulse' special effect on whatever we just touched, just to give the user some feedback
cpFloat originalScale_ = cpCCSOwner.scale;
CCFiniteTimeAction *zoomAction = [CCScaleTo actionWithDuration:0.1f scale:originalScale_ * 1.2f];// zoom in
CCFiniteTimeAction *shrinkAction = [CCScaleTo actionWithDuration:0.1f scale:originalScale_];// zoom out
CCSequence *actions = [CCSequence actions:zoomAction, shrinkAction, nil];// zoom in, then zoom out
[cpCCSOwner runAction:actions];// now
// Since I just grabbed the object, it should stop moving
cpCCSOwner.shape->body->v = cpvzero;
// Let's keep track of what we are touching.
cpCCSpriteTouched = cpCCSOwner; //<- I just added this to the class, you'll need something more sophisticated from non-trivial cases
} else {
NSLog(@"nothing was actually touched - you missed");
cpCCSpriteTouched = nil;
}
return YES;
}