//
// Dir.cpp
// yhhTankTest
//
// Created by luonan on 14-5-24.
//
//
#include "Dir.h"
USING_NS_CC;
cocos2d::CCScene* Dir:: scene(){
static CCScene*s = CCScene::create();
CCSprite *controlSprite=CCSprite::create("Icon-57.png");
Dir* joystick=Dir::DirWithCenter(ccp(200.0f,200.0f),60.0f ,controlSprite,false);
s->addChild(joystick,1);
return s;
}
bool Dir::init(){
CCLayer::init();
this->scheduleUpdate();
return true;
}
void Dir::updatePos(CCTime dt){
jsSprite->setPosition(ccpAdd(jsSprite->getPosition(),ccpMult(ccpSub(currentPoint, jsSprite->getPosition()),0.5)));
}
void Dir::Active()
{
if (!active) {
active=true;
schedule(schedule_selector(Dir::updatePos));
CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, 0,false);
}else {
}
}
void Dir::Inactive()
{
if (active) {
active=false;
this->unschedule(schedule_selector(Dir::updatePos));
CCDirector::sharedDirector()->getTouchDispatcher()->removeDelegate(this);
}else {
}
}
int Dir::curDir = Dir::up;
CCPoint Dir::getDirection()
{
CCPoint p = ccpNormalize(ccpSub(centerPoint, currentPoint));
// CCLOG("p.x== > %f,p.y ==> %f",p.x,p.y);
// CCLOG("centerPoint.x==>%f,centerPoint.y == >%f",centerPoint.x,centerPoint.y);
// CCLOG("currentPoint.x==>%f,currentPoint.y == >%f",currentPoint.x,currentPoint.y);
float k = currentPoint.x/currentPoint.y;
CCLOG("f = %f",k);
if (k>0.5&&k<=1 ) {
if (p.y<=0) {
curDir = up;
}else{
curDir = left;
}
}
if (k>1&&k<=1.56) {
if (-0.5<p.x&& p.x<=0.7) {
curDir = down;
}else{
curDir = right;
}
}
return p;
}
void Dir::update(float dt){
CCLayer::update(dt);
getDirection();
}
float Dir::getVelocity()
{
return ccpDistance(centerPoint, currentPoint);
}
Dir* Dir:: DirWithCenter(CCPoint aPoint ,float aRadius ,CCSprite* aJsSprite,bool _isFollowRole){
Dir *jstick=Dir::create();
jstick->initWithCenter(aPoint,aRadius,aJsSprite,_isFollowRole);
return jstick;
}
bool Dir::ccTouchBegan(CCTouch* touch, CCEvent* event)
{
if (!active)
return false;
this->setVisible(true);
CCPoint touchPoint = touch->getLocationInView();
touchPoint = CCDirector:: sharedDirector()->convertToGL(touchPoint);
if(!isFollowRole){
if (ccpDistance(touchPoint, centerPoint) > radius){
return false;
}
}
currentPoint = touchPoint;
if(isFollowRole){
centerPoint=currentPoint;
jsSprite->setPosition(currentPoint);
this->getChildByTag(88)->setPosition(currentPoint);
}
return true;
}
void Dir::ccTouchMoved(CCTouch* touch, CCEvent* event)
{
CCPoint touchPoint = touch->getLocationInView();
touchPoint = CCDirector:: sharedDirector()->convertToGL(touchPoint);
if (ccpDistance(touchPoint, centerPoint) > radius)
{
currentPoint =ccpAdd(centerPoint,ccpMult(ccpNormalize(ccpSub(touchPoint, centerPoint)), radius));
}else {
currentPoint = touchPoint;
}
}
void Dir::ccTouchEnded(CCTouch* touch, CCEvent* event)
{
currentPoint = centerPoint;
if(isFollowRole){
this->setVisible(false);
}
}
Dir* Dir::initWithCenter(CCPoint aPoint ,float aRadius ,CCSprite* aJsSprite,bool _isFollowRole){
isFollowRole =_isFollowRole;
active = false;
radius = aRadius;
if(!_isFollowRole){
centerPoint =aPoint;
}else{
centerPoint =ccp(0,0);
}
currentPoint = centerPoint;
jsSprite = aJsSprite;
jsSprite->setPosition(centerPoint);
this->addChild(jsSprite);
if(isFollowRole){
this->setVisible(false);
}
this->Active();
return this;
}
评论2
最新资源