3
クリックしたときにCocos2dのメニューボタンの位置を取得する方法はありますか?メニューアイテムがクリックされたときにメニューアイテムの位置を取得する(関数に渡す)
だから私のメニューがあります。HelloWorld.m
HelloWorld.h
//creating a menu
CCMenu *menu;
を
// initializing the menu and its position
menu = [CCMenu menuWithItems:nil];
menu.position = ccp(0,0);
// set cells in placing grid
[self setItem];
[self addChild:menu];
- (void)setItem
{
//this method is a loop that creates menu items
//but i've simplified it for this example, but please keep in mind that there are lots
//of menu items and tagging them could be troublesome
for (int i = 1; i <= 13; i++) {
for (int j = 1; j <= 8; j++) {
// this creates a menu item called grid
CCMenuItem grid = [CCMenuItemSprite
itemWithNormalSprite:[CCSprite spriteWithSpriteFrameName:@"menuItem.png"]
selectedSprite:[CCSprite spriteWithSpriteFrameName:@"selected.png"]
target:self
// when button is pressed go to someSelector function
selector:@selector(someSelector:)];
//coordinates
float x = (j+0.55) * grid.contentSize.width;
float y = (i-0.5) * grid.contentSize.height;
//passing the coordinates
grid.position = ccp(x, y);
//add grid to the menu
[menu addChild:grid];
//loop unless finished
}
}
}
-(void)someSelector:(id)selector
{
//i know when the button is pressed but is there any way
//to pass selected menu coordinates to this function?
NSLog(@"Grid is pressed");
}
は基本的に上記されて、何が起こるか - 私は、メニューを作成し、その後、私は関数を呼び出しますこれらのメニュー項目が作成されると、それがメニューに追加されます。各メニュー項目は自己のターゲットを持っていて、selectorはsomeSelector関数です。これはパラメータを(メニューボタンの位置)に渡したいものです。私はここで何をしたいのか
され、
私はシミュレータでプログラムを実行すると、私が押されたメニューボタンの位置を取得できるようにしたいです。
ありがとうございます、あなたからのご意見をお待ちしております。
私は自分の質問に対する解決策見つけたと思う:
NSLog(@"Grid is pressed %f %f", item.position.x, item.position.y);
出来上がり:
-(void)someSelector:(id)selector
が
-(void)someSelector:(CCMenuItem *) item
に変更すると、あなたはこれを行うことができています! :)あなたのハンドラで
素晴らしいhspainです!どうもありがとう。ちょっと私にそれを説明できますか?だからセレクタは、押されているメニュー項目に割り当てられている何らかの種類のIDですか?それは正しい? – Eugene
実際には「送信者」と言い、自分の応答を更新しました。 senderパラメータは、押されたメニュー項目への参照です。イベントを呼び出すものが分かっているので、CCMenuItemにキャストできます。 – hspain