好的,所以我開始瞭解更多關於Coco2D的知識,但我有點受傷。我發現的很多教程都是針對過時的代碼版本的,所以當我仔細查看它們是如何做某些事情的時候,我無法將它翻譯成我自己的程序,因爲很多內容已經改變。據說,我正在使用最新版本的Coco2d 0.99版本。如何讓對象對Cocos2D中的觸摸做出反應?
我想要做的是在屏幕上創建一個精靈(完成),然後當我觸摸該精靈時,我可以有「事情」發生。現在,我們只是讓警報熄滅。現在,我得到了這個代碼在朋友的幫助下工作。這裏是頭文件:
// When you import this file, you import all the cocos2d classes
#import "cocos2d.h"
// HelloWorld Layer
@interface HelloWorld : CCLayer
{
CGRect spRect;
}
// returns a Scene that contains the HelloWorld as the only child
+(id) scene;
@end
這裏是實現文件:
//
// cocos2d Hello World example
// http://www.cocos2d-iphone.org
//
// Import the interfaces
#import "HelloWorldScene.h"
#import "CustomCCNode.h"
// HelloWorld implementation
@implementation HelloWorld
+(id) scene
{
// 'scene' is an autorelease object.
CCScene *scene = [CCScene node];
// 'layer' is an autorelease object.
HelloWorld *layer = [HelloWorld node];
// add layer as a child to scene
[scene addChild: layer];
// return the scene
return scene;
}
// on "init" you need to initialize your instance
-(id) init
{
// always call "super" init
// Apple recommends to re-assign "self" with the "super" return value
if((self=[super init])) {
// create and initialize a Label
CCLabel* label = [CCLabel labelWithString:@"Hello World" fontName:@"Times New Roman" fontSize:64];
// ask director the the window size
CGSize size = [[CCDirector sharedDirector] winSize];
// position the label on the center of the screen
label.position = ccp(size.width /2 , size.height/2);
// add the label as a child to this Layer
[self addChild: label];
CCSprite *sp = [CCSprite spriteWithFile:@"test2.png"];
sp.position = ccp(300,200);
[self addChild:sp];
float w = [sp contentSize].width;
float h = [sp contentSize].height;
CGPoint aPoint = CGPointMake([sp position].x - (w/2), [sp position].y - (h/2));
spRect = CGRectMake(aPoint.x, aPoint.y, w, h);
CCSprite *sprite2 = [CCSprite spriteWithFile:@"test3.png"];
sprite2.position = ccp(100,100);
[self addChild:sprite2];
//[self registerWithTouchDispatcher];
self.isTouchEnabled = YES;
}
return self;
}
// on "dealloc" you need to release all your retained objects
- (void) dealloc
{
// in case you have something to dealloc, do it in this method
// in this particular example nothing needs to be released.
// cocos2d will automatically release all the children (Label)
// don't forget to call "super dealloc"
[super dealloc];
}
- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
//CGPoint location = [[CCDirector sharedDirector] convertCoordinate:[touch locationInView:touch.view]];
CGPoint location = [touch locationInView:[touch view]];
location = [[CCDirector sharedDirector] convertToGL:location];
if (CGRectContainsPoint(spRect, location)) {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"Win"
message:@"testing"
delegate:nil cancelButtonTitle:@"okay"
otherButtonTitles:nil];
[alert show];
[alert release];
NSLog(@"TOUCHES");
}
NSLog(@"Touch got");
}
然而,這僅適用於1個對象,我創建的CGRect的精靈。我無法做到2個精靈,我正在測試。所以我的問題是這樣的:我怎麼能讓屏幕上的所有精靈在觸摸時對同一事件做出反應?
對於我的程序,需要針對同一類型的所有對象運行相同的事件,以便讓它更容易一點。我嘗試了製作CCNode的一個子類並重寫了這個方法,但那根本不起作用......所以我做錯了什麼。幫助將不勝感激!
翻閱cocos2D中的「Touches」項目,看看我看到他們是如何做到的。它看起來像他們做了一個子類,並覆蓋了方法:
- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
- (void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event
- (void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event
所以現在我得弄清楚爲什麼我的不行...嗯...
那麼你如何實例化這個新的類與一個PNG文件例如? – 2010-04-23 16:04:17
你還需要知道嗎?這只是CCSprite的一個子類,所以: CCSprite * aSprite = [CCSprite spriteWIthTexture:[在CCTexture中添加png]]; – 2010-07-09 18:18:35
您也可以使用boundingBox屬性而不是自定義rect方法。 – 2011-04-27 10:50:25