本人开发的开发者技术变现资源聚集地,大家支持下,下面是网址
https://www.baiydu.com
一、主要实现
今天spritekit实现创建玩家角色精灵(SKSpriteNode *), 增加角色精灵的手势操作,这里增加的手势计算方法与objective-c中是不一样的,因为objective-c使用的坐标系与spritekit使用的坐标系不是一样的,后面还增加了精灵的碰撞检查代码。
二、 SKSpriteNode手势
SKSpriteNode类自带5个手势监测的方法,
// 手指按下的时候调用
1、 -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event // 手指移动的时候调用 2、 -(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
// 手指抬起的时候调用
3、- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
取消(非正常离开屏幕,意外中断事件)
4、 -(void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
3D Touch相关方法,当前触摸对象估计的触摸特性,返回值是UITouchPropertyie、
5、 -(void)touchesEstimatedPropertiesUpdated:(NSSet *)touches
6、上面5个方法中用得最多的是 1、2、3,下面我们要操作的角色精灵也是用到这三个手势,实现的思路→当用户点击屏幕时进入到1手势,判断点击的坐标点是不是在角色精灵精灵上,如果是才能执行2手势,代码中用了一个int变量纪录,如果点击到了角色精灵int=1,int=1时2手势才能执行,当用户手抬起时,将会执行手势3,说明手势结束,结束后我们将int=1设置为=0。
二、 代码
1.场景层初始化中增加创建角色精灵的代码:SKSpriteNode * FirendPlane
UIImage *RolePlaneImage=[UIImage imageNamed:@"AttackPlane"];
SKTexture *RolePlaneImageTextture = [SKTexture textureWithImage:RolePlaneImage];
FirendPlane=[SKSpriteNode spriteNodeWithTexture:RolePlaneImageTextture size:CGSizeMake(DEVICE_Width*0.25, DEVICE_Width*0.25)];
FirendPlane.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:FirendPlane.size];
/*增加碰撞监测代码*/
FirendPlane.physicsBody.categoryBitMask = SKRoleCategoryFoePlane;
FirendPlane.zPosition=;
FirendPlane.position=CGPointMake(self.frame.size.width/, );
[self addChild:FirendPlane];
2.增加手势
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
UITouch *touctObj = [touches anyObject]; CGPoint location=[touctObj locationInNode:self]; CGFloat CurrentTagAreaX=FirendPlane.position.x-FirendPlane.size.width/; CGFloat CurrentTagAreaY=FirendPlane.position.y-FirendPlane.size.height/;
if (location.x>=CurrentTagAreaX &&location.x<=FirendPlane.position.x+(FirendPlane.size.width/) &&
location.y>=CurrentTagAreaY &&location.y<=FirendPlane.position.y+(FirendPlane.size.height/)) { IsOrNoTachMyPlane=;
} } - (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
IsOrNoTachMyPlane=; } -(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
if (IsOrNoTachMyPlane>) {
UITouch *touctObj = [touches anyObject];
CGPoint location=[touctObj locationInNode:self]; if (location.x >= self.size.width - (FirendPlane.size.width / )) { location.x = self.size.width - (FirendPlane.size.width / ); }else if (location.x <= (FirendPlane.size.width / )) { location.x = FirendPlane.position.x; } if (location.y >= self.size.height - (FirendPlane.size.height / )) { location.y = self.size.height - (FirendPlane.size.height / ); }else if (location.y <= (FirendPlane.size.height / )) { location.y = FirendPlane.position.y; } SKAction *action = [SKAction moveTo:CGPointMake(location.x, location.y) duration:]; [FirendPlane runAction:action];
}
二、 下载地址
http://download.csdn.net/detail/liaohang1987x/9610880
本人创业做的一款androidApp, 下载量已经有2000多万,各种当前热门的网络手机奖励红包全部集成,另外还有热门电影和淘宝高额优惠券!很适合各类型的用户。