Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix several issues #1

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Xcode
.DS_Store
*/build/*
*.pbxuser
!default.pbxuser
*.mode1v3
!default.mode1v3
*.mode2v3
!default.mode2v3
*.perspectivev3
!default.perspectivev3
xcuserdata
profile
*.moved-aside
DerivedData
.idea/
*.hmap

#CocoaPods
Pods
12 changes: 9 additions & 3 deletions PlaneWar.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
A40FF94D17C1F8AF00CA29BB /* Common.m in Sources */ = {isa = PBXBuildFile; fileRef = A40FF94C17C1F8AF00CA29BB /* Common.m */; };
A9466EDF17BDDC9E009232EA /* gameArts-hd.plist in Resources */ = {isa = PBXBuildFile; fileRef = A9466EDD17BDDC9E009232EA /* gameArts-hd.plist */; };
A9466EE017BDDC9E009232EA /* gameArts-hd.png in Resources */ = {isa = PBXBuildFile; fileRef = A9466EDE17BDDC9E009232EA /* gameArts-hd.png */; };
A948CDCF17BDD5A300BB9EC9 /* QuartzCore.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = A948CDCE17BDD5A300BB9EC9 /* QuartzCore.framework */; };
Expand Down Expand Up @@ -149,6 +150,8 @@
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
A40FF94B17C1F8AF00CA29BB /* Common.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Common.h; sourceTree = "<group>"; };
A40FF94C17C1F8AF00CA29BB /* Common.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Common.m; sourceTree = "<group>"; };
A9466EDD17BDDC9E009232EA /* gameArts-hd.plist */ = {isa = PBXFileReference; lastKnownFileType = file.bplist; path = "gameArts-hd.plist"; sourceTree = "<group>"; };
A9466EDE17BDDC9E009232EA /* gameArts-hd.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "gameArts-hd.png"; sourceTree = "<group>"; };
A948CDCB17BDD5A300BB9EC9 /* PlaneWar.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = PlaneWar.app; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -498,14 +501,16 @@
children = (
A948CF7317BDD5A500BB9EC9 /* AppDelegate.h */,
A948CF7417BDD5A500BB9EC9 /* AppDelegate.m */,
A948CF7617BDD5A500BB9EC9 /* HelloWorldLayer.h */,
A948CF7717BDD5A500BB9EC9 /* HelloWorldLayer.m */,
A40FF94B17C1F8AF00CA29BB /* Common.h */,
A40FF94C17C1F8AF00CA29BB /* Common.m */,
A948CF8117BDD5F200BB9EC9 /* GameLayer.h */,
A948CF8217BDD5F200BB9EC9 /* GameLayer.m */,
A948CF7617BDD5A500BB9EC9 /* HelloWorldLayer.h */,
A948CF7717BDD5A500BB9EC9 /* HelloWorldLayer.m */,
A948CF7917BDD5A500BB9EC9 /* IntroLayer.h */,
A948CF7A17BDD5A500BB9EC9 /* IntroLayer.m */,
A948CDE317BDD5A300BB9EC9 /* Resources */,
A948CDFB17BDD5A300BB9EC9 /* libs */,
A948CDE317BDD5A300BB9EC9 /* Resources */,
A948CF6F17BDD5A500BB9EC9 /* Supporting Files */,
);
path = PlaneWar;
Expand Down Expand Up @@ -1086,6 +1091,7 @@
A948CF7817BDD5A500BB9EC9 /* HelloWorldLayer.m in Sources */,
A948CF7B17BDD5A500BB9EC9 /* IntroLayer.m in Sources */,
A948CF8317BDD5F200BB9EC9 /* GameLayer.m in Sources */,
A40FF94D17C1F8AF00CA29BB /* Common.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
13 changes: 13 additions & 0 deletions PlaneWar/Common.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// Common.h
// PlaneWar
//
// Created by jy on 13-8-19.
// Copyright (c) 2013年 GetToSet. All rights reserved.
//

#import <Foundation/Foundation.h>

CGFloat randomFloatRange(CGFloat begin, CGFloat end);
NSInteger randomIntRange(NSInteger begin, NSInteger end);

19 changes: 19 additions & 0 deletions PlaneWar/Common.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// Common.m
// PlaneWar
//
// Created by jy on 13-8-19.
// Copyright (c) 2013年 GetToSet. All rights reserved.
//
#import "Common.h"

#define ARC4RANDOM_MAX 0x100000000

CGFloat randomFloatRange(CGFloat begin, CGFloat end){
CGFloat range = end - begin;
return ((CGFloat)arc4random() / ARC4RANDOM_MAX) * range + begin;
}

NSInteger randomIntRange(NSInteger begin, NSInteger end){
return arc4random()%(end-begin+1)+begin;
}
2 changes: 2 additions & 0 deletions PlaneWar/GameLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
CCSprite *playerPlane;

CCSprite *bomb;

CCSprite *pause;

CCLabelTTF *scoreLabel;

Expand Down
47 changes: 37 additions & 10 deletions PlaneWar/GameLayer.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,20 @@
//

#import "GameLayer.h"
#import "Common.h"

@implementation GameLayer

/**
* 防止出现短时间内击中敌机使得其HP<0导致的type改变
* 原理:tag = type * 100 + hp,
* 如果敌机type = 2 ,hp = 0,在系统未完成敌机死亡检测时再次被击中,则从tag计算得出的type变成1
* 加上Buffer后,即使hp变成-1,tag值依然大于type * 100
* 计算hp时减去Buffer即可
* PS 依然是ugly hack
*/
static NSInteger hpBuffer = 10;

//基本方法

+(CCScene*)scene{
Expand All @@ -34,6 +45,7 @@ -(id)init{
[self startShowEnemies];
[self startCheckCollision];
[self loadBombButton];
[self loadPauseButton];
[self startShowProps];
[self loadScoreLabel];
}
Expand Down Expand Up @@ -168,11 +180,11 @@ -(void)showEnemy{

CCSprite *enemy=[CCSprite spriteWithSpriteFrameName:[NSString stringWithFormat:@"enemy%i_fly_1.png",type]];
enemy.anchorPoint=ccp(0.5,0);
enemy.position=ccp(arc4random()%(int)(winSize.width+1),winSize.height);
enemy.position=ccp(randomFloatRange(20.0, winSize.width-20.0),winSize.height);
[self addChild:enemy z:4];

//Tag用于记录敌机类型和HP值(这比再写一个类要方便多了)
[enemy setTag:type*100+hp];
[enemy setTag:type*100+hp+hpBuffer];

[enemies addObject:enemy];

Expand All @@ -181,7 +193,7 @@ -(void)showEnemy{
[enemy runAction:action];
}

id enemyMoveDown=[CCMoveBy actionWithDuration:5.0f position:ccp(0,-winSize.height-enemy.boundingBox.size.height)];
id enemyMoveDown=[CCMoveBy actionWithDuration:(randomFloatRange(3.0, 6.0)) position:ccp(0,-winSize.height-enemy.boundingBox.size.height)];
id enemyMoveEnd=[CCCallFuncND actionWithTarget:self selector:@selector(enemyMoveEndedWithAction:Sprite:) data:enemy];

[enemy runAction:[CCSequence actions:enemyMoveDown,enemyMoveEnd,nil]];
Expand Down Expand Up @@ -395,6 +407,14 @@ -(void)loadBombButton{
[bomb setVisible:NO];
}

-(void)loadPauseButton{
pause=[CCSprite spriteWithSpriteFrameName:@"game_pause.png"];
pause.anchorPoint = ccp(0,0);
pause.position = ccp(winSize.width*0.85, winSize.height*0.9);
[self addChild:pause];
[pause setVisible:YES];
}

//触摸处理

-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
Expand All @@ -415,11 +435,20 @@ -(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

[enemies removeAllObjects];
}
}else if (CGRectContainsPoint(pause.boundingBox, touchLocation)){
if ([[CCDirector sharedDirector] isPaused]) {
[[CCDirector sharedDirector] resume];
}else{
[[CCDirector sharedDirector] pause];
}
}
}

-(void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{

if ([[CCDirector sharedDirector] isPaused]) {
return;
}

UITouch *touch=[touches anyObject];
CGPoint touchLocation=[touch locationInView:touch.view];
touchLocation=[[CCDirector sharedDirector]convertToGL:touchLocation];
Expand All @@ -429,11 +458,9 @@ -(void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{

CGPoint translation=ccpSub(touchLocation,oldTouchLocation);

if(CGRectContainsPoint(playerPlane.boundingBox,touchLocation)){
CGPoint newPos = ccpAdd(playerPlane.position,translation);
if(CGRectContainsRect(CGRectMake(0,0,winSize.width,winSize.height),[self newRectWithSize:playerPlane.boundingBox.size Point:newPos AnchorPoint:ccp(0.5,0.5)])){
playerPlane.position = newPos;
}
CGPoint newPos = ccpAdd(playerPlane.position,translation);
if(CGRectContainsRect(CGRectMake(0,0,winSize.width,winSize.height),[self newRectWithSize:playerPlane.boundingBox.size Point:newPos AnchorPoint:ccp(0.5,0.5)])){
playerPlane.position = newPos;
}
}

Expand Down Expand Up @@ -469,7 +496,7 @@ -(int)getEnemyTypeWithTag:(NSUInteger)tag{
}

-(int)getEnemyHpWithTag:(NSUInteger)tag{
return tag%100;
return tag%100-hpBuffer;
}

//对话框回调
Expand Down