您的位置:首页 > 其它

克服困难,解决遇到的问题!!

2011-08-17 17:37 239 查看
遇到了一些问题,一度产生放弃的想法,不过好歹最后还是一路坚持了下来,将问题解决了大半~

反观以前做圆角边框的时候,要用程序解大量的曲线方程,算得我真是想吐,也是想放弃,不过还是挺到最后把这块骨头啃了下来

这次做的是一个将所有形状放缩成同一高度的功能,遇到的一些困难,都是因为粗心大意所致,

不过也是有一些麻烦的地方,麻烦地我不想去做,反正今天是不像做了,残缺地功能等我哪天心情好了就把他收了。

现在游戏地样貌已经开始慢慢地付出水面,我负责了大部分地代码编辑,好友负责出游戏图片和游戏思路

这个游戏其实也就是山寨地一款 flash游戏,名叫 super stack,本来是不怎么情愿做山寨地东西地

可无奈技术有限,刚刚进入iphone游戏开发领域,所以随便捡个还算有些可玩性地游戏拿出来山寨山寨~

今后会以自己地游戏创意,技巧和美工来开发一款真正属于自己地,精品地游戏!!

不过在这之前,还是地踏踏实实地学好objective-c,xcode以及一些常用游戏框架,比如cocos2d,box2d,

今后还会进军3d游戏,效益如果ok地话,这是肯定地!!对3d游戏向往很久了,能搞出一个3d游戏来地话应该还是比较有成就感地

当然,现在正在做地这个2d游戏也让我很有成就感!!fighting!!

晒一晒今天写地一点儿东西,只在一念之间,这点儿东西的不玩全体就差点被投进trash了~

很庆幸自己坚持了下来,希望今后的自己也能一直这样,做事执着不轻易放弃。

即使当时遇到了一点儿困难让自己心烦以乱 ,也可以先将问题放一放,说不定某个时候再回头看的时候,一下子就把问题给解决了!!

是的,一定是这样的!!

ToolBar.h

//
//  ToolBar.h
//  SuperBalance1.1
//
//  Created by Eric Zhu && Bruce Yang on 8/13/11.
//  Copyright 2011 Home. All rights reserved.
//

#import "cocos2d.h"
#import "BYShape.h"

@interface ToolBar : CCNode {
NSMutableArray *_shapes;
BOOL _isPickedUp;
CCSprite *_title;

// 注意: CCSprite 对象的 contentSize 即使在 scale 以后也是不会发生改变的~
float _previousShapeHalfWidth;
float _currentShapeScale;
}

@property (nonatomic, retain) NSMutableArray *shapes;

-(void) setShapes:(NSMutableArray*)shapes;

-(BYShape*) getFirstShape;

-(void) beganTouchArea:(float)x y:(float)y;

-(void) endTouchArea:(float)x y:(float)y;

-(void) removeFirstShape;

@end


ToolBar.mm

//
//  ToolBar.mm
//  SuperBalance1.1
//
//  Created by Eric Zhu && Bruce Yang on 8/13/11.
//  Copyright 2011 Home. All rights reserved.
//

#import "ToolBar.h"
#import "GameConfig.h"

@implementation ToolBar

@synthesize shapes = _shapes;

-(id) init {
if ( (self=[super init]) ) {
_previousShapeHalfWidth = 0.0f;

_title = [CCSprite spriteWithFile:@"game_scene_title.png"];	// add bg_img to title
_title.position = ccp(160,458);
[self addChild:_title];
}
return self;
}

-(void) setShapes:(NSMutableArray*)shapes {
_shapes = shapes;
int startX = 360;
int deltaX = 40;
int i = 0;
int distance2center = 0;
BOOL flag = YES;
for(BYShape *shape in _shapes) {
CCSprite *sprite = [shape getSprite];
int spriteHeight = sprite.contentSize.height;	// 形状的高度
float transformRatio = (float)TOOL_BAR_HEIGHT / (float)spriteHeight;	// 比率~

if(transformRatio > 1.0f) {	// 如果形状本身的高度就小于 TOOL_BAR_HEIGHT 的话,不进行任何放缩以避免画质降低~
transformRatio = 1.0f;
}

// 保留1位小数
//		NSString *reserveOneFloatPart = [NSString stringWithFormat:@"%0.1f", transformRatio];
//		transformRatio = [reserveOneFloatPart floatValue];
//		NSLog(@"保留1位小数: %.2f", transformRatio);

sprite.position = ccp(startX + deltaX * i, 460);
[sprite runAction:[CCScaleTo actionWithDuration:0.0f scale:transformRatio]];

int spriteHalfWidth = sprite.contentSize.width * transformRatio / 2;
if(i == 0) {	// 必须的(之前形状向中间移动的时候对不齐原因就在于没加上这句代码)!!!
// 竟然会出现 transformRatio 和 sprite.scale 不相等的情况,奇葩(可能源于数据还未同步过来)~
_previousShapeHalfWidth = spriteHalfWidth;	// 第一个形状的一半宽度
}

if(flag == YES) {
flag = NO;
} else {
distance2center += spriteHalfWidth;
distance2center += TOOL_BAR_INTERVAL;	// 两个形状之间的间隔距离~
}
float distance = 160.0f + distance2center;
[sprite runAction:[CCMoveTo actionWithDuration:0.6f position:ccp(distance, 460.0f)]];
distance2center += spriteHalfWidth;
i += 1;
[self addChild:sprite];
}
}

-(void) beganTouchArea:(float)x y:(float)y {
if ( (_title.position.x+40)>x && (_title.position.x-40)<x && (_title.position.y-60)<y) {
if([_shapes count] != 0) {
BYShape *shape = (BYShape*)[_shapes objectAtIndex:0];
// 如果对于某个 shape 是第一次拾取,则演示放大效果(增加1个判断,修正了储物栏中形状移动不及时的bug)~
float shapeX = [shape getSprite].position.x;
if(shapeX >=120 && shapeX <= 200 && _isPickedUp == NO) {
_isPickedUp = YES;
// 在进行放大变换之前,将该形状的 scale 备份一下,到时候放回去的时候才有个缩小的依据~
_currentShapeScale = [shape getSprite].scale;
NSLog(@"_currentShapeScale: %f", _currentShapeScale);
[[shape getSprite] runAction: [CCScaleTo actionWithDuration:0.1f scale:1.0f]];
}
}
}
}

-(void) endTouchArea:(float)x y:(float)y {
if ( (_title.position.x+40)>x && (_title.position.x-40)<x && (_title.position.y-60)<y) {
if([_shapes count] != 0) {
BYShape *shape = (BYShape*)[_shapes objectAtIndex:0];
_isPickedUp = NO;
[[shape getSprite] runAction:[CCMoveTo actionWithDuration:0.1f position:ccp(_title.position.x, 460)]];
[[shape getSprite] runAction:[CCScaleTo actionWithDuration:0.1f scale:_currentShapeScale]];
}
} else if (_isPickedUp == YES) {
CCSprite *shapeSprite = [[self getFirstShape] getSprite];
[(BYShape*)shapeSprite.userData addSprite2b2World:ccp(x, y)];
[self removeFirstShape];
}
}

-(void) removeFirstShape {
if([_shapes count] != 0) {
_isPickedUp = NO;
[_shapes removeObjectAtIndex:0];
if([_shapes count] != 0) {
CCSprite *sprite = [[_shapes objectAtIndex:0] getSprite];
float currentShapeHalfWidth = (sprite.contentSize.width) * (sprite.scale) / 2;
float moveDistance = _previousShapeHalfWidth + TOOL_BAR_INTERVAL + currentShapeHalfWidth;
_previousShapeHalfWidth = currentShapeHalfWidth;

for(BYShape *shape in _shapes) {	//add action moveto center position
CCSprite *sprite = [shape getSprite];
[sprite runAction:[CCMoveBy actionWithDuration:0.1 position:ccp(-moveDistance, 0)]];
}
}
}
}

-(BYShape*) getFirstShape {
if([_shapes count] != 0 && _isPickedUp == YES) {
BYShape *shape = (BYShape*)[_shapes objectAtIndex:0];
return shape;
} else {
return nil;
}
}

-(void) dealloc {
// _shapes是在GameScene中分配的堆内存,之前已经在GameScene的dealloc方法中被dealloc掉了,
// 这里再 dealloc 的话会报出 “EXC_BAD_ACCESS” 的错误,这种情形今后应该小心对待~
//	[_shapes dealloc];
[super dealloc];
}

@end


其实老早就想把游戏的截图传到博客里面来了,不过由于不知到如何在mac book 里面将 ipod touch里面的图片取出来,所以。。。唉,悲催啊~
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息