2012-02-07 58 views
0

我正在開發一個cocos2d遊戲。在那個遊戲中,我擁有一個角色,根據角色上的觸摸位置,應該觸發不同的動作,我如何在我的cocos2d遊戲中實現這一點。 是否有任何方法來實現透明按鈕在cocos2d。在cocos2d中創建transperent按鈕iphone

在此先感謝

回答

3

當你創建一個CCMenuItemSprite(按鈕),你爲它分配一個精靈使用的顯示器。

然後,您可以通過更改精靈的不透明屬性或使其完全不可見來更改按鈕的外觀。

CCSprite *buttonSpr = [CCSprite spriteWithSpriteFrameName:@"spr.png"]; 

CCMenuItem *button = [CCMenuItemSprite itemFromNormalSprite:buttonSpr selectedSprite:buttonSpr target:self selector:@selector(buttonTapped:)]; 

//opacity 
buttonSpr.opacity = 50; 

//invisible 
buttonSpr.visible = false; 
0

我並不完全相信,我明白這個問題,更多的信息會有所幫助,但我會盡我所能回答它。

假設你有一個字符類我會實現checkTouchesBegan,做這樣的事情:

-(BOOL) checkTouchesBegan: (CGPoint*) location 
{ 
//conver the touch coordinates to fit your system 
int converty = location->y-160; 
int convertx = location->x-240; 

//determine where the touch is in relation to the center of the character 
float ydif = (1.0)*(converty - character_y); 
float xdif = (1.0)*(convertx - character_x); 

//determine the angle of the touch 
float degrees = atan2f(xdif, ydif) * 57; 
//determine the distance between the character and the touch 
float squared = xdif*xdif + ydif*ydif; 

//if the touch is above the character and within a certain distance 
if(degrees >= 45 && degrees < 135 && sqrt(squared) < 100) 
{ 
    doSomething; 
    return YES; 
} 
//if the touch is below the character and within a certain distance 
else if(degrees < -45 && degrees >= -135 && sqrt(squared) < 100) 
{ 
doSomething; 
return YES; 
} 
//if the touch is to the right of the character and within a certain distance 
else if(degrees >= -45 && degrees < 45 && sqrt(squared) < 100) 
{ 
doSomething; 
return YES; 
} 
//if the touch is to the left of the character and within a certain distance 
else if(sqrt(squared) < 100) 
{ 
doSomething; 
return YES; 
} 
return NO; 
} 

希望這有助於一些!