小黑米的生活

只玩技术

Archive for the ‘objective-c’ Category

[cocos2d] 游戏里面的场景过渡

leave a comment

1. scene: OtherScene
2. init: <OtherScene = 066B2130 | Tag = -1>
3. onEnter: <OtherScene = 066B2130 | Tag = -1>
4. // 这里运行了过渡效果
5. onExit: <FirstScene = 0668DF40 | Tag = -1>
6. onEnterTransitionDidFinish: <OtherScene = 066B2130 | Tag = -1>
7. dealloc: <FirstScene = 0668DF40 | Tag = -1>

上面的是cocos2d里面的场景过渡日志,可以发现很有趣的,A场景过渡到B场景的时候,A场景发起replaceScene请求,就init B场景,然后执行B场景的onEnter函数,程序执行到这里,就说明B场景init完成,然后对A场景执行onExit和dealloc请求,最后请求A场景的onEnterTransitionDidFinish,这样就完成了2个场景

继续阅读

Written by edwardhey

十月 6th, 2011 at 7:45 上午

Posted in 技术,游戏

[cocos2d] OpenGL draw base functions

leave a comment

-(void) draw
{
CGSize s = [[CCDirector sharedDirector] winSize];

// draw a simple line
// The default state is:
// Line Width: 1
// color: 255,255,255,255 (white, non-transparent)
// Anti-Aliased
glEnable(GL_LINE_SMOOTH);

ccDrawLine( ccp(0, 0), ccp(s.width, s.height) );

// line: color, width, aliased
// glLineWidth > 1 and GL_LINE_SMOOTH are not compatible
// GL_SMOOTH_LINE_WIDTH_RANGE = (1,1) on iPhone
glDisable(GL_LINE_SMOOTH);
glLineWidth( 5.0f );
glColor4ub(255,0,0,255);
ccDrawLine( ccp(0

//以下被省略了…

继续阅读

Written by edwardhey

十月 6th, 2011 at 12:04 上午

Posted in objective-c,技术

[cocos2d] CCTransitionScene

leave a comment

CCTransitionScene – 所有过渡效果的基类

//淡入淡出
CCFadTransition *tran = [CCFadTransition transitionWithDuration:1
scene:[HelloWorld scene]
withColor:ccWHITE];

[[CCDirector sharedDirector] replaceScene:tran];
CCFadTransition: 淡入淡出到一个指定颜色,然后再回来
CCFadTRTransition(3):瓦片(tiles)反转过来提示场景
CCJumpZoomTransition:场景跳动着变小时,新场景则跳动着变大
MoveInLTransition(3):场景移出,同时新的场景从左(3)移入.

CCOrientedTransitionScene(6):

//以下被省略了…

继续阅读

Written by edwardhey

十月 5th, 2011 at 9:16 下午

Posted in 技术,游戏

[cocos2d] cocos2d里面一些实用的东西

leave a comment

允许屏保

[self setScreenSaverEnabled:YES];
#pragma mark Reset Game
// The game is played only using the accelerometer. The screen may go dark while playing because the player
// won’t touch the screen. This method allows the screensaver to be disabled during gameplay.
-(void) setScreenSaverEnabled:(bool)enabled
{
UIApplication *thisApp = [UIApplication sharedApplication];
thisApp.idleTimerDisabled = !enabled;
}

启用重力感应

self.isAccelerometerEnabled = YES;

启用触摸屏

self.isTouchEnabled = NO;

foreac


//以下被省略了…

继续阅读

Written by edwardhey

十月 5th, 2011 at 8:32 下午

Posted in objective-c,技术

[cocos2d] schedule

leave a comment

在处理Sprite的时候,需要设置Sprite的行为,需要使用到schedule,第一个参数就是selector对象,额~ 理解成C语言的函数指针就对了~

[self unschedule:@selector(spidersUpdate:)];
[self schedule:@selector(spidersUpdate:) interval:3.0f];
[self scheduleUpdate];//==[self scheduleUpdate:@selector(update:) interval:0.0f];

继续阅读

Written by edwardhey

十月 4th, 2011 at 10:04 下午

Posted in 技术,游戏

[cocos2d] CCArray类

leave a comment

你可 以在Xcode项目中的cocos2d/Support组找到CCArray的类文件。cocos2d引擎内 部使用了CCArray。它和苹果官方的NSMutableArray类似,但是运行效率更高。 CCArray类实现了NSArray和NSMutableArray的子集,并且添加一些从NSArray初 始化到CCArray的新方法。CCArray通过将数组中的最后一个对象(nil)赋值给删 除的位置,实现了fastRemoveObject和fastRemoveObjectAtIndex两个方法。避 免了复制部份数组的内存。这让删除数组中的元素变的更快,不过这也意味着 CCArray中的对象会改变位置。如果你的程序依赖于指定的对象排列次序,你就 不应该使用fastRemoveObject这类方法。在列表4-9中,你可以看到CCArray的 类引用 – 它没有实现所有的NSArray和NSMutableArray方法:
列表4-9. CCArray的类引用

+ (id) array;
+ (id) arrayWithCapacity:(NSUInteger)capacity; + (i

//以下被省略了…

继续阅读

Written by edwardhey

十月 4th, 2011 at 4:56 下午

[cocos2d] 加速计的使用

leave a comment

以下代码使用了三个新的“设计参数”:减速度 速率,加速计敏感度和最大速度。

减速是通过降低当前速度值来实现的。在降低速度值以后,要将新的加速度值和加速计敏感度值相乘所得的值与降低后的速度值相加。减速度速率越小,主角精灵就能越快的改变蜘蛛的方向。加速计敏感度的值越大,主角精灵对加速计的输入就越敏感。因为这三个参数一起影响着同一个值,所以在调整的时候要注意每次只调整一个值。

在layer的init加入

[self scheduleUpdate];

player是玩家的精灵,playervelocity

-(void) update:(ccTime) delta
{
CGPoint pos = player.position;
pos.x += playervelocity.x;

CGSize screenSize = [[CCDirector sharedDirector] winSize];
float imageWidthHalved = [player texture].contentSize.width*0.5f;
float le

//以下被省略了…

继续阅读

Written by edwardhey

十月 4th, 2011 at 4:23 下午

Posted in 技术,游戏

[cocos2d] cocos2d 1.0 设置屏幕默认为纵向

leave a comment

在RootViewController.m里面,在shouldAutorotateToInterfaceOrientation:方法里面,找到这个宏判断

#elif GAME_AUTOROTATION == kGameAutorotationUIViewController

然后把

return (UIInterfaceOrientationIsLandscape(interfaceOrientation));

改成

return (UIInterfaceOrientationIsPortrait(interfaceOrientation));

继续阅读

Written by edwardhey

十月 4th, 2011 at 1:22 下午

Posted in objective-c,游戏

[cocos2d] 游戏开发基础概念

leave a comment

场景(CCScene)
每个场景都是通过不同的局(Layer)的叠加和组合协作来实现丌同的功能的。因此,通常每个场景都是有一个戒者几个局组成的。

场景(CCScene)
层是我们写游戏的重点,我们大约 99%以上的时间是在局上实现我们游戏内容。层有叠加顺序,最上层的将会最先响应时间,如果有一个层处理了该时间,则排在后面的层讲不再接收到该时间

精灵(CCSprite)
精灵是整个游戏开发处理的主要对象,地方的飞机、坦克是系统AI控制的精灵。

导演(CCDirector)
导演负责游戏全过程的场景切换,通常只有一个。

继续阅读

Written by edwardhey

十月 3rd, 2011 at 11:41 下午

Posted in objective-c,游戏

[Object-C] 指针函数

leave a comment

//
// main.m
// obc
//
// Created by edward yang on 11-6-12.
// Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import <Foundation/Foundation.h>

int lookup (void){
return 1;
}

int main (int argc, const char * argv[])
{

NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

//声明为函数指针
int (*fnPtr) (void);
fnPtr = lookup;

NSLog(@“%d,fnPtr());

[pool drain];
return 0;
}

继续阅读

Written by edwardhey

六月 16th, 2011 at 11:15 上午

Posted in objective-c