标签:游戏 类 实例 foundation框架 function
ios射击类游戏简单代码射击
之后会持续的更新,学习的同学请关注,共同学习
main.c
#import <Foundation/Foundation.h> #import "Soldier.h" int main(int argc, const char * argv[]) { Soldier *s1 = [[Soldier alloc]init]; s1->_life = 100; s1->_name = @"007"; Gun * gun = [[Gun alloc]init]; [s1 fireByGun:gun]; // NSLog(@"%@",s1); // NSLog(@"%d %@",s1->_life,s1->_name); return 0; }
Gun.m
#import "Gun.h" @implementation Gun -(void)shoot { _bulletCount--; NSLog(@"剩余的子弹数量%d",_bulletCount); } @end
Gun,h
#import <Foundation/Foundation.h> @interface Gun : NSObject { @public /**子弹的数量*/ int _bulletCount; } //发射 -(void)shoot; @end
Soldier.m
#import "Soldier.h" @implementation Soldier //射击 -(void)fireByGun:(Gun *)gun; { [gun shoot];//[nil shoot];nil = 0 oc中给nil发送消息不会产生异常 } @end
Soldier.h
#import <Foundation/Foundation.h> #import "Gun.h" @interface Soldier : NSObject { @public /**姓名*/ NSString * _name; /**生命值*/ int _life; // /**用于存储枪支对象的实例变量*/ // Gun * _gun; } //开枪 -(void)fireByGun:(Gun *)gun; @end
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:游戏 类 实例 foundation框架 function
原文地址:http://blog.csdn.net/u012701023/article/details/46910889