網頁

2010年7月19日 星期一

Cocoa-Invoking function

Cocoa-調用函式
Last Update: 2010/07/19 16:35


使用NSObject 的 performSelector 來調用函式
SEL 是一個指標結構,用來儲存函式指標



typedef struct {
int code;
NSString *message;
}PassingData;

- (void)invokedMethod : (id)sender : (PassingData*)msg{
NSLog(@"%d : %@", msg->code, msg->message);
}

- (void)execInvoke : (id)sender : (SEL)method{
PassingData *pd = malloc(sizeof(PassingData));
pd->code = 3;
pd->message = @"QWERT";
[sender performSelector:method withObject:sender withObject:(id)pd];
free(pd);
}

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
// Insert code here to initialize your application 
[self execInvoke:self :@selector(invokedMethod::)];
}

2010年7月14日 星期三

iPhone-Cocos2d Progress/Health Bar

iPhone - Cocos2d 進度 / 血量 條
Last Update: 2010/07/14 21:18



前置


環境:
XCode 3.2.2
Compile: Simulator 3.1.3-Debug
iPhone SDK 4.0
Cocos2d 0.99.4

準備工作:
外框及內部圖片


實作






新增Cocos2d專案以後
將圖片加入Resource

我們將用 CCProgressTimer 來做進度 / 血量 條
把下列程式覆寫在 init 函式裡的 if 區段



CCSprite *spriteBorder = [CCSprite spriteWithFile:@"hp_border.png"];
spriteBorder.position = ccp(150,150);
[self addChild:spriteBorder];

先放個外框


CCProgressTimer *spriteBar = [CCProgressTimer progressWithFile:@"hp_bar.png"];
spriteBar.type = kCCProgressTimerTypeHorizontalBarLR;
[spriteBorder addChild:spriteBar];
[spriteBar setAnchorPoint:ccp(0,0)];

.type 選擇水平從左到右
將 Bar 加入外框的子節點
並將它 "Anchor" 在(0, 0)的位置
這樣省了計算外框和 Bar 的關係位置


spriteBar.percentage = 80;
CCAction *action = [CCProgressTo actionWithDuration:1 percent:1];
[spriteBar runAction:action];

2 種方式變更計量
直接設定 以及 CCProgressTo (action)




iPhone-Cocos2d Animation Frame 2

iPhone-Cocos2d 動畫 (連續圖片播放) part.2
Last Update: 2010/07/14 20:43

前置


環境:
XCode 3.2.2
Cocos2d 0.99.4

準備工作:
連續圖片


步驟


新增Cocos2d專案
將圖片直接加入Resource
將 init 函式裡的 Code 用下列程式碼代換
滿淺顯易懂的, 就不說明了


if( (self=[super init] )) {


CCSprite *mySprite = [[CCSprite alloc] initWithFile:@"fox_0001.png"];
CCSprite *mySprite2 = [[CCSprite alloc] initWithFile:@"fox_0001.png"];


CCAnimation *walk = [[CCAnimation alloc] initWithName:@"walk" delay:0.1];
for(int i=1;i<=10;i++){
[walk addFrameWithFilename:[NSString stringWithFormat:@"fox_%04d.png", i]];
}


[mySprite addAnimation:walk];
[mySprite2 addAnimation:walk];

//[mySprite setDisplayFrame:@"walk" index:0];
//[mySprite setDisplayFrame:@"walk" index:1];

CCAction *action = [CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:walk restoreOriginalFrame:NO]];
CCAction *action2 = [CCAnimate actionWithAnimation:walk restoreOriginalFrame:NO];
[mySprite runAction:action];
[mySprite2 runAction:action2];


mySprite.position = ccp(150,150);
mySprite2.position = ccp(300,150);

[self addChild:mySprite];
[self addChild:mySprite2];
}
return self;



2010年7月6日 星期二

iPhone-Cocos2d Other

雜項 其它 未整理


向量:
ccpLength
ccpLengSQ
ccpDistance
ccSub





Call function
CCCallFuncND eventFunc = [CCCallFuncND
actionWithTarget:self
selector:@selector(gotoGameScene:data:)
data:role];
[eventFunc execute];





CCSequence, dynamically add action

+(CCFiniteTimeAction *) getActionSequence: (NSArray *) actions
{
CCFiniteTimeAction *seq = nil;
for (CCFiniteTimeAction *anAction in actions)
{
if (!seq){
seq = anAction;
}else{
seq = [CCSequence actionOne:seq two:anAction];
}
}
return seq;
}








自定義資料
CCNode.tag
CCNode.userData