zoukankan      html  css  js  c++  java
  • cocos2d触碰例子代码

     1 //
     2 //  TestLayer.h
     3 //  MiniTD
     4 //
     5 //  Created by OnePiece on 12-7-30.
     6 //  Copyright 2012年 __MyCompanyName__. All rights reserved.
     7 //
     8 
     9 #import <Foundation/Foundation.h>
    10 #import "cocos2d.h"
    11 #import "CCCrack.h"
    12 
    13 @interface TestLayer : CCNode
    14 {
    15     CCCrack *crack;
    16 }
    17 +(id)scene;
    18 @end
      1 //
      2 //  TestLayer.m
      3 //  MiniTD
      4 //
      5 //  Created by OnePiece on 12-7-30.
      6 //  Copyright 2012年 __MyCompanyName__. All rights reserved.
      7 //
      8 
      9 #import "TestLayer.h"
     10 #import "CCExtendLabel.h"
     11 #import "BaseDefine.h"
     12 #import "CCCrack.h"
     13 #import "UpgradeNode.h"
     14 @interface TestLayer()<UIGestureRecognizerDelegate>{
     15     UpgradeNode *upgradeNode;
     16     int  buildStep;
     17     CCSprite *sprite;
     18 
     19 }
     20 @end
     21 @implementation TestLayer
     22 +(id)scene
     23 {
     24     CCScene *scene = [CCScene node];
     25     TestLayer *testLayer = [TestLayer node];
     26     [scene addChild:testLayer];
     27     
     28     
     29     
     30     
     31     return scene;
     32 }
     33 //多指操作看来最好还是用真机测试,测试各种手势之间的冲突情况
     34 -(void)onEnter{
     35     [super onEnter];
     36     
     37     sprite=[[CCSprite alloc] initWithFile:@"gege.png"];
     38     sprite.position=ccp(200,200);
     39     [self addChild:sprite];
     40     
     41     
     42    // [self buildingProcess];
     43     
     44     
     45   UIPanGestureRecognizer *  panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanFrom:)];
     46     panRecognizer.delegate = self;
     47     
     48    // panRecognizer.cancelsTouchesInView=NO;
     49  //  panRecognizer.delaysTouchesBegan=YES;
     50    
     51     int fff= panRecognizer.maximumNumberOfTouches; //=2;
     52     int fdsfdsf= panRecognizer.minimumNumberOfTouches; //=2;
     53     
     54     //panRecognizer.minimumNumberOfTouches=2;
     55    // panRecognizer.maximumNumberOfTouches=2;
     56     UILongPressGestureRecognizer *pressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handlePressFrom:)];
     57     pressGesture.minimumPressDuration = 1;
     58     
     59     pressGesture.allowableMovement = 30;
     60     
     61     pressGesture.delaysTouchesBegan=YES;
     62     
     63     pressGesture.cancelsTouchesInView=NO;
     64     
     65     
     66     pressGesture.delegate = self;
     67     
     68 //    int ff= pressGesture.numberOfTapsRequired;
     69 //    int ffff= pressGesture.numberOfTouchesRequired;
     70 //   
     71   //  pressGesture.numberOfTouchesRequired=2;
     72    //  pressGesture.numberOfTapsRequired=0;
     73    
     74     
     75    [[[CCDirector sharedDirector]openGLView]addGestureRecognizer:panRecognizer];
     76   
     77     
     78     [[[CCDirector sharedDirector]openGLView]addGestureRecognizer:pressGesture];
     79 
     80     
     81   
     82     
     83 
     84     
     85 
     86 
     87 }
     88 -(void)handlePressFrom:(UILongPressGestureRecognizer *)recognizer
     89 {
     90     
     91     
     92     if([recognizer state] == UIGestureRecognizerStateBegan)
     93     {
     94         printf("长按开始
    ");
     95         
     96     }
     97     else if([recognizer state] == UIGestureRecognizerStateChanged)
     98     {
     99         
    100      printf("长按移动
    ");
    101     }
    102     else if([recognizer state] == UIGestureRecognizerStateEnded)
    103     {
    104     printf("长按结束
    ");
    105     }
    106 }
    107 
    108 
    109 //地图的平移
    110 - (void)handlePanFrom:(UIPanGestureRecognizer *)recognizer {
    111     
    112     
    113     if(recognizer.state == UIGestureRecognizerStateBegan)
    114     {
    115         printf("开始位移
    ");
    116     }
    117     else if(recognizer.state == UIGestureRecognizerStateChanged)
    118     {
    119        printf("位移中
    ");
    120     }
    121     else if (recognizer.state == UIGestureRecognizerStateEnded)
    122     {
    123          printf("位移结束
    ");
    124     }
    125 }
    126 
    127 
    128 //- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
    129 //{
    130 //    return YES;
    131 //}
    132 //-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
    133 //{
    134 //    return YES;
    135 //}
    136 
    137 
    138 
    139 
    140 
    141 
    142 
    143 -(void)buildingProcess;
    144 {
    145     if(!upgradeNode)
    146     {
    147         buildStep = 1;
    148         upgradeNode = [UpgradeNode nodeWithParent:self Level:3];
    149         upgradeNode.position = ccp(200,200);
    150         [self addChild:upgradeNode z:MaxZorder];
    151         [upgradeNode startProgress];
    152         
    153         //        //建造过程特效
    154         //        CCSprite *buildEffectSprite = [CCSprite spriteWithSpriteFrameName:@"military_build_1.png"];
    155         //        buildEffectSprite.opacity = 1;
    156         //        buildEffectSprite.position = ccp(buildEffectSprite.contentSize.width/2,buildEffectSprite.contentSize.height/2);
    157         //        [mainSprite addChild:buildEffectSprite z:1 tag:200];
    158         //        [buildEffectSprite runAction:[CCFadeIn actionWithDuration:1.6]];
    159         //
    160         sprite.opacity=0;
    161         [sprite runAction:[CCFadeIn actionWithDuration:10]];
    162         
    163        
    164         
    165         
    166         [self schedule:_cmd]; //调用自身方法
    167         
    168         
    169     }
    170     
    171     //    // 2秒为啥和1.6*3对应
    172     //
    173     int percentage = [upgradeNode percentage];
    174     
    175     if(buildStep == 1 && percentage >= 32)
    176     {
    177         buildStep = 2;
    178         
    179         //        mainSprite.opacity = 255;
    180         //        //很聪明啊,通过叠加的效果
    181         //        [mainSprite setDisplayFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"military_build_1.png"]];
    182         //
    183         //        CCSprite *buildEffectSprite = (CCSprite*)[mainSprite getChildByTag:200];
    184         //        [buildEffectSprite setDisplayFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"military_build_2.png"]];
    185         
    186          [sprite setDisplayFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"military_build_2.png"]];
    187        
    188         sprite.opacity = 0;
    189         
    190         [sprite runAction:[CCFadeIn actionWithDuration:2]];
    191         
    192         [self unschedule:_cmd]; //停止轮训自身
    193         
    194     }
    195     //    else if(buildStep == 2 && percentage >= 64)
    196     //    {
    197     //        buildStep = 3;
    198     //
    199     //        [mainSprite setDisplayFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"military_build_2.png"]];
    200     //
    201     //        CCSprite *buildEffectSprite = (CCSprite*)[mainSprite getChildByTag:200];
    202     //        [buildEffectSprite setDisplayFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"militaryTower2.png"]];
    203     //        buildEffectSprite.opacity = 0;
    204     //        [buildEffectSprite runAction:[CCFadeIn actionWithDuration:1.6]];
    205     //    }
    206     //
    207     //
    208     //    if(buildStep ==3 && percentage >= 100)
    209     //    {
    210     //
    211     //
    212     //
    213     //        buildStep = -1; //建好了
    214     //
    215     //        [upgradeNode endProgress];//移除loading
    216     //        upgradeNode = nil;//置空
    217     //
    218     //        buildComplete = true; //建塔完毕
    219     //
    220     //        [mainSprite removeChildByTag:200 cleanup:YES];
    221     //
    222     //        CCSpriteFrame *frame = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"militaryTower2.png"];
    223     //        [mainSprite setDisplayFrame:frame];
    224     //        mainSprite.position = ccp(17,-33);
    225     //
    226     //
    227     //        //升级的效果
    228     //        [self upgradeSmoke];//烟雾
    229     //
    230     //        [self unschedule:_cmd]; //停止轮训自身
    231     //
    232     //
    233     //
    234     //
    235     //
    236     //
    237     //        isCanUpgradeMainTower = true;//可以升级主建筑
    238     //
    239     //        [self UpgradeBuildWithLevel:curUpgradeLevel Property:curPropertyCount Brach:hybrid Skill:(SkillStruct){0,0}];
    240     //
    241     //        //音乐先忽略
    242     //        //        [SoundManager stopEffect:buildEffectID];
    243     //        //        [SoundManager playEffect:Military_Build];
    244     //
    245     //
    246     //
    247     //
    248     //
    249     //        zorderPos = ccpAdd(baseSprite.position,ccp(0,-45));
    250     //        [self.parent reorderChild:self z:[user zorderForPosition:zorderPos]];
    251     //
    252     //        [self focusAnimation:@"LostFocus"];
    253     //
    254     //        //检测是否需要显示锁定精灵
    255     //        if(curUpgradeLevel == maxUpgradeLevel)
    256     //        {
    257     //            lockSprite.visible = YES;
    258     //        }
    259     //
    260     //
    261     //        //检查士兵 是否移动到新的旗子点
    262     //        [self schedule:@selector(scheduleSoldier) interval:0.5f];
    263     //        
    264     //        //实时检测是否需要更新胡萝卜数
    265     //        [self scheduleUpdate];
    266     //        
    267     //        //更新buff
    268     //        [self schedule:@selector(updateBuff) interval:0.2];
    269     //        
    270     //        [user addTower:self];
    271     //        
    272     //        //增加一个建塔数成就点
    273     //        [[AchievementManager sharedManager] addTowerType:Military_Tower Count:1 TowerLevel:1];
    274     //    }
    275 }
    276 
    277 
    278 -(id)init
    279 {
    280     if(self = [super init])
    281     {
    282        [[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:-2 swallowsTouches:YES];
    283     }
    284     
    285     return self;
    286 }
    287 
    288 -(BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
    289 {
    290 //    crack = [CCCrack bladeWithMaximumPoint:100];
    291 //    [crack setTexture:[[CCTextureCache sharedTextureCache] addImage:@"streak.png"]];
    292 //    [self addChild:crack];
    293      printf("轻点开始
    ");
    294     return YES;
    295 }
    296 
    297 -(void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event
    298 {
    299 //    CGPoint location = [self convertTouchToNodeSpace:touch];
    300 //    
    301 //    [crack push:location];
    302     
    303      printf("轻点滑动
    ");
    304 }
    305 
    306 -(void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event
    307 {
    308       printf("轻点结束
    ");
    309 }
    310 -(void) ccTouchCancelled:(UITouch *)touch withEvent:(UIEvent *)event
    311 {
    312     printf("触碰取消
    ");
    313 }
    314 
    315 
    316 @end
     [[CCDirector sharedDirector] runWithScene:[CCTransitionFade transitionWithDuration:0.5 scene:[TestLayer scene] withColor:ccBLACK]];
  • 相关阅读:
    是否可以在tomcat servlet中禁用jsessionid?
    一个屌丝程序猿的人生(一百二十一)
    postman 使用
    【Ubuntu】命令行安装deb安装包
    opencv的cascade分类器源码分析
    Face Detection – OpenCV, Dlib and Deep Learning ( C++ / Python )
    小样本目标检测研究现状
    图像特征提取三大法宝:HOG特征,LBP特征,Haar特征
    搞清楚nand flash和 nor flash 以及 spi flash 和cfi flash 的区别
    xhsell远程vmware ubuntu16.04
  • 原文地址:https://www.cnblogs.com/xiaonanxia/p/5764027.html
Copyright © 2011-2022 走看看