Cocos2d-x中动作类的扩展

头文件:CCActionEx.h

#pragma once

#include "..\..\..\cocos2dx\actions\CCActionInterval.h"//包含系统延时类动作头文件
using namespace cocos2d;

/***********************************************
Name:CircleAction
Desc:圆运动轨迹
Auth:Ivan_han@2013-04-23
***********************************************/
/*
   LUA中使用方法:
   --轨迹起点
   self:setPosition(CCPoint(200,300));
   --圆逆时针运动轨迹,参数一为动画时长,参数二为当前圆上位置的对径点坐标,参数三为轨迹方向
   self:runAction(CircleAction:create(3.0, ccp(500,300),1));

    --轨迹起点
    self:setPosition(CCPoint(200,300));
    --圆顺时针运动轨迹,参数一为动画时长,参数二为当前圆上位置的对径点坐标,参数三为轨迹方向
    self:runAction(CircleAction:create(3.0, ccp(500,300),-1));
*/
class  CircleAction : public CCActionInterval
{
public:
    /** initializes the action */
    bool initWithDuration(float duration, const CCPoint& position,int direction);
 
    virtual void startWithTarget(CCNode *pTarget);
    virtual void update(float time);
 
public:
    /** creates the action */
    static CircleAction* create(float duration, const CCPoint& position,int direction);
 
protected: 
    CCPoint m_startPosition;//轨迹起点
    CCPoint m_endPosition;//轨迹终点
 int m_direction;//1表示逆时针Anti-clockwise,-1表示顺时针Clockwise
};

/***********************************************
Name:myAction
Desc:任意运动轨迹,轨迹的参数方程写在LUA脚本中
Auth:Ivan_han@2013-04-23
***********************************************/
/*
   LUA中使用方法:
   --自定义运动轨迹,参数一为动画时长,参数二为表示轨迹参数方程的LUA函数名
   self:runAction(myAction:create(3.0,"CircleTrace"));

   --圆中心为(350,300),半径为150,轨迹起点为(500,300)的逆时针圆轨迹
   --圆运动轨迹,参数一为动画时长,参数二为表示圆轨迹参数方程的LUA函数名,参数三为轨迹起点坐标,参数四为圆心坐标,参数五为正负圈数
   self:runAction(myAction:createCircle(3.0,"CircleTraceFunc",ccp(500,300),ccp(350,300),1));
*/
class  myAction : public CCActionInterval
{
public:
 myAction(){memset(m_TraceFunc,0,sizeof(m_TraceFunc));m_TraceType=0;}

    /** initializes the action */
    bool initWithDuration(float duration,const char *func);
 
    virtual void startWithTarget(CCNode *pTarget);
    virtual void update(float time);
 
public:
    /** creates the action */
 //相当于CC**To
    static myAction* create(float duration,const char *func);
 //带有控制参数的createXXX函数,相当于CC**By
 static myAction* createCircle(float duration,const char *func,const CCPoint& startP,const CCPoint& centerP,float direction);
 
protected:
 char m_TraceFunc[20];//LUA函数名,定义了轨迹的参数方程
 int m_TraceType;//0表示create,1表示createCircle
 //针对圆轨迹的成员变量
    CCPoint m_startP;//轨迹起点
    CCPoint m_centerP;//轨迹中心点
 float m_direction;//1表示逆时针Anti-clockwise,-1表示顺时针Clockwise
};

实现文件CCActionEx.cpp:

 

#include "CCActionEx.h"
#include "..\..\..\cocos2dx\support\CCPointExtension.h"
#include "..\..\..\scripting\lua\cocos2dx_support\CCLuaEngine.h"
#include "..\..\..\scripting\lua\tolua\tolua++.h"

void CircleAction::startWithTarget(CCNode *pTarget){
    CCActionInterval::startWithTarget(pTarget);
    m_startPosition = pTarget->getPosition();//轨迹起点
}
 
CircleAction* CircleAction::create(float duration, const CCPoint& position,int direction){
    CircleAction * pmyAction=new CircleAction();
    pmyAction->initWithDuration(duration, position,direction);
    pmyAction->autorelease();
 
    return pmyAction;
}
//这个函数得到初始化数据
bool CircleAction::initWithDuration(float duration, const CCPoint& position,int direction){
    if (CCActionInterval::initWithDuration(duration))
    {
        m_endPosition = position; //轨迹终点,对于圆轨迹,是半圆弧轨迹的终点
  m_direction =direction;
        return true;
    }
 
    return false;
}
void CircleAction::update(float time){
    if (m_pTarget)
    {
        //这里的time就相当于当前时间占总的需要运行时间的百分比
        /*
   这里轨迹就用一个圆
   x=cos(t),y=sin(t),当时间走完就是一圈360°
   对于圆轨迹,圆心坐标C=(m_startPosition+m_endPosition)/2
        */
      CCPoint C=ccp((m_startPosition.x+m_endPosition.x)*0.5,(m_startPosition.y+m_endPosition.y)*0.5);//圆心
   float startarg=atan2(m_startPosition.y-C.y,m_startPosition.x-C.x);//轨迹起点相对于圆心的辐角主值∈(-pi,pi]
      float tempx=(m_startPosition.x-m_endPosition.x)*(m_startPosition.x-m_endPosition.x);
   float tempy=(m_startPosition.y-m_endPosition.y)*(m_startPosition.y-m_endPosition.y);
   float r=sqrt(tempx+tempy)*0.5;//圆的半径
   float x=r*cos(startarg+2*M_PI*time*m_direction);//圆上点相对于圆心的X坐标偏移
   float y=r*sin(startarg+2*M_PI*time*m_direction);//圆上点相对于圆心的Y坐标偏移
   m_pTarget->setPosition(ccpAdd(C,ccp(x,y)));
    }
}

void myAction::startWithTarget(CCNode *pTarget){
    CCActionInterval::startWithTarget(pTarget);
}
 
myAction* myAction::create(float duration,const char *func){
    myAction * pmyAction=new myAction();
    pmyAction->initWithDuration(duration,func);
    pmyAction->autorelease();
 
    return pmyAction;
}
//这个函数得到初始化数据
bool myAction::initWithDuration(float duration,const char *func){
    if (CCActionInterval::initWithDuration(duration))
    {
  strcpy(m_TraceFunc,func);
        return true;
    }
    return false;
}

//带有控制参数的createXXX函数
myAction* myAction::createCircle(float duration,const char *func,const CCPoint& startP,const CCPoint& centerP,float direction)
{
    myAction * pmyAction=new myAction();
    if(pmyAction==NULL)
    {
        return NULL;
    }
 if(pmyAction->CCActionInterval::initWithDuration(duration)==false)
    {
        return NULL;
    }
 strcpy(pmyAction->m_TraceFunc,func);
 pmyAction->m_TraceType=1;
 pmyAction->m_startP=startP;
 pmyAction->m_centerP=centerP;
 pmyAction->m_direction=direction;
    pmyAction->autorelease();
 
    return pmyAction;
}


void myAction::update(float time){
    if (m_pTarget && m_TraceFunc)
    {
  if(m_TraceType==0)
  {
    //这里的time就相当于当前时间占总的需要运行时间的百分比
    CCLuaEngine* pEngine = CCLuaEngine::defaultEngine();
    lua_State* m_state = pEngine->getLuaState();
    //int recv=pEngine->executeGlobalFunction(m_TraceFunc,1);
    //函数名
    lua_getglobal(m_state,m_TraceFunc);
    //第一个参数压栈
    lua_pushnumber(m_state, time);
    //调用函数
    lua_call(m_state, 1, 1);
    //得到返回值
    CCPoint retP=*(CCPoint*)(tolua_tousertype(m_state,2,0));
    lua_pop(m_state, 1);

    m_pTarget->setPosition(retP);
    return;
  }
  if(m_TraceType==1)
  {
    //这里的time就相当于当前时间占总的需要运行时间的百分比
    CCLuaEngine* pEngine = CCLuaEngine::defaultEngine();
    lua_State* m_state = pEngine->getLuaState();
    //函数名
    lua_getglobal(m_state,m_TraceFunc);
    //第一个参数压栈
    lua_pushnumber(m_state, time);
    //第二三个参数压栈
    //lua_pushlightuserdata(m_state, &m_startP);
    //void *lp=lua_newuserdata(m_state,sizeof(CCPoint));
    //memcpy(lp,&m_startP,sizeof(CCPoint));
    lua_pushnumber(m_state,m_startP.x);
    lua_pushnumber(m_state,m_startP.y);
    //第四五个参数压栈
    //lua_pushlightuserdata(m_state, &m_centerP);
    //void *lp1=lua_newuserdata(m_state,sizeof(CCPoint));
    //memcpy(lp1,&m_centerP,sizeof(CCPoint));
    lua_pushnumber(m_state,m_centerP.x);
    lua_pushnumber(m_state,m_centerP.y);
    //第六个参数压栈
    lua_pushnumber(m_state,m_direction);
    //调用函数
    lua_call(m_state,6, 1);
    //得到返回值
    CCPoint retP=*(CCPoint*)(tolua_tousertype(m_state,2,0));
    lua_pop(m_state, 1);

    m_pTarget->setPosition(retP);
    return;
  }


    }
}

tolua++用以自动生成LUA<->C++交互接口文件L_CCActionEx.h的pkg文件CCActionEx.pkg:

#pragma once

class  CircleAction : public CCActionInterval
{
    /** creates the action */
    static CircleAction* create(float duration, const CCPoint& position,int direction);
};

class  myAction : public CCActionInterval
{
    /** creates the action */
    static myAction* create(float duration,const char *func);
    static myAction* createCircle(float duration,const char *func,const CCPoint& startP,const CCPoint& centerP,float direction);
};

LUA中使用代码:

 

--[[
三阶贝塞尔曲线(P_0,P_1,P_2,P_3)退化为二阶贝塞尔曲线(P_0,P_1,P_2,即抛物线:起点P_0=控制点P_1,控制点P_2,终点P_3)
参数说明:
startP----轨迹起点
endP----轨迹终点
startAngle----
endAngle----
time----动画时长
]]
function Fish:moveWithParabola(startP,endP,startAngle,endAngle,time)
 local sx = startP.x;
 local sy = startP.y;
 local ex =endP.x;
 local ey =endP.y;

 local h = self:getContentSize().height * 0.5;
 local pos=CCPoint(sx,sy);
 self:setPosition(pos);
 self:setRotation(startAngle);

 -- 贝塞尔曲线
 local bezier=ccBezierConfig:new();
 -- 控制点1(起点)
 bezier.controlPoint_1 = ccp(sx, sy);
 -- 控制点2
 --bezier.controlPoint_2 = ccp(sx+(ex-sx)*0.5, sy+(ey-sy)*0.5);
 bezier.controlPoint_2 = ccp(sx+(ex-sx)*0.5, sy+(ey-sy)*0.5+100); 
 -- 终点
 bezier.endPosition = ccp(endP.x, endP.y);

 local actionMove = CCBezierTo:create(time, bezier);
 local actionRotate = CCRotateTo:create(time, endAngle);
 local action = CCSpawn:createWithTwoActions(actionMove, actionRotate);
 local pcc=CCCallFunc:create(function()
          self:removeFromParentAndCleanup(true)
    end)
 local sq = CCSequence:createWithTwoActions(action,pcc); 
 self:runAction(sq);
end

function CircleTrace(t)
          --圆中心为(350,300),半径为150,轨迹起点为(500,300)的逆时针圆轨迹
          local x=150*math.cos(2*math.pi*t)
          local y=150*math.sin(2*math.pi*t)
          return ccp(350+x,300+y)
end

function EllipseTrace(t)
          --椭圆中心为(350,300),长半径为150,短半径为120,轨迹起点为(500,300),焦点F_1为(260,300),焦点F_2为(440,300)的逆时针椭圆轨迹
          local C=ccp(350,300)
          local a=150
          local b=120
          local c=90
          local x=a*math.cos(2*math.pi*t)
          local y=b*math.sin(2*math.pi*t)
          return ccp(C.x+x,C.y+y)
end

function SinTrace(t)
          --轨迹起点为(500,300),振幅为150的正弦曲线轨迹
          local C=ccp(350,300)
          local a=150
          local x=a*2*math.pi*t
          local y=a*math.sin(2*math.pi*t)
          return ccp(C.x+x,C.y+y)
end

--[[
圆轨迹
参数说明:
t----轨迹参数方程中的参数
startP----轨迹起点坐标
centerP----圆心坐标
direction----取值浮点数,表示圈数,正、负圈数分别表示逆、顺时针旋转
]]
--function CircleTraceFunc(t,startP,centerP,direction)
function CircleTraceFunc(t,startPx,startPy,centerPx,centerPy,direction)
          local startP=ccp(startPx,startPy)
          local centerP=ccp(centerPx,centerPy)
          --圆中心为centerP,半径为r,轨迹起点为startP的direction弧度圆轨迹
          local startarg=math.atan2(startP.y-centerP.y,startP.x-centerP.x);--轨迹起点相对于圆心的辐角主值
          local tempx=(startP.x-centerP.x)*(startP.x-centerP.x);
    local tempy=(startP.y-centerP.y)*(startP.y-centerP.y);
    local r=math.sqrt(tempx+tempy);--圆的半径
          local x=r*math.cos(startarg+2*math.pi*t*direction)
          local y=r*math.sin(startarg+2*math.pi*t*direction)
          return ccp(centerP.x+x,centerP.y+y)
end

function Fish:addPath(pathType)
if(pathType==1)then
   self:moveWithParabola(ccp(200, 300), ccp(500, 300), 0.0, 20.0, 3.0);
end
if(pathType==2)then
   self:setPosition(CCPoint(200,300)); 
    --创建一个移动动画,内部调用create实现。参数一为动画时长,参数二为要移动到的目标点。 
   self:runAction(CCMoveTo:create(3.0, ccp(500,300)));
   --self:runAction(CCRotateTo:create(1, 180.0));
end 
if(pathType==3)then
   --轨迹起点
   self:setPosition(CCPoint(200,300));
   --圆逆时针运动轨迹,参数一为动画时长,参数二为当前圆上位置的对径点坐标,参数三为轨迹方向
   self:runAction(CircleAction:create(3.0, ccp(500,300),1));
end
if(pathType==4)then
   --轨迹起点
   self:setPosition(CCPoint(200,300));
   --圆顺时针运动轨迹,参数一为动画时长,参数二为当前圆上位置的对径点坐标,参数三为轨迹方向
   self:runAction(CircleAction:create(3.0, ccp(500,300),-1));
end
if(pathType==5)then
   --自定义运动轨迹,参数一为动画时长,参数二为表示轨迹参数方程的LUA函数名
   --self:runAction(myAction:create(3.0,"CircleTrace"));
   self:runAction(myAction:createCircle(3.0,"CircleTraceFunc",ccp(500,300),ccp(350,300),2));
end
if(pathType==6)then
   --圆中心为(350,300),半径为150,轨迹起点为(500,300)的逆时针圆轨迹
   --圆运动轨迹,参数一为动画时长,参数二为表示圆轨迹参数方程的LUA函数名,参数三为轨迹起点坐标,参数四为圆心坐标,参数五为正负圈数
   self:runAction(myAction:createCircle(3.0,"CircleTraceFunc",ccp(500,300),ccp(350,300),1));
end
if(pathType==7)then
   --自定义运动轨迹,参数一为动画时长,参数二为表示轨迹参数方程的LUA函数名
   self:runAction(myAction:create(3.0,"EllipseTrace"));
end
if(pathType==8)then
   --自定义运动轨迹,参数一为动画时长,参数二为表示轨迹参数方程的LUA函数名
   self:runAction(myAction:create(3.0,"SinTrace"));
end    
end

 


5月底改为无LUA轻型框架后:
#pragma once

#include "..\..\..\cocos2dx\actions\CCActionInterval.h"//包含系统延时类动作头文件
using namespace cocos2d;

//add by Ivan_han 20130528
typedef void(*pFuncVoid)(void);
typedef CCPoint(*pFuncTrace0)(float t);
typedef CCPoint(*pFuncTrace1)(float t,float startPx,float startPy,float r,float deltArg,float direction,float reservedx,float reservedy);

/***********************************************
Name:CircleAction
Desc:圆运动轨迹
Auth:Ivan_han@2013-04-23
***********************************************/
/*
   LUA中使用方法:
   --轨迹起点
   self:setPosition(CCPoint(200,300));
   --圆逆时针运动轨迹,参数一为动画时长,参数二为当前圆上位置的对径点坐标,参数三为轨迹方向
   self:runAction(CircleAction:create(3.0, ccp(500,300),1));

    --轨迹起点
    self:setPosition(CCPoint(200,300));
    --圆顺时针运动轨迹,参数一为动画时长,参数二为当前圆上位置的对径点坐标,参数三为轨迹方向
    self:runAction(CircleAction:create(3.0, ccp(500,300),-1));
*/
class  CircleAction : public CCActionInterval
{
public:
    /** initializes the action */
    bool initWithDuration(float duration, const CCPoint& position,int direction);
 
    virtual void startWithTarget(CCNode *pTarget);
    virtual void update(float time);
 
public:
    /** creates the action */
    static CircleAction* create(float duration, const CCPoint& position,int direction);
 
protected: 
    CCPoint m_startPosition;//轨迹起点
    CCPoint m_endPosition;//轨迹终点
 int m_direction;//1表示逆时针Anti-clockwise,-1表示顺时针Clockwise
};

/***********************************************
Name:myAction
Desc:任意运动轨迹,轨迹的参数方程写在LUA脚本中
Auth:Ivan_han@2013-04-23
***********************************************/
/*
LUA中使用方法:
--自定义运动轨迹,参数一为动画时长,参数二为表示轨迹参数方程的LUA函数名
self:runAction(myAction:create(3.0,"CircleTrace"));

--圆中心为(350,300),半径为150,轨迹起点为(500,300)的逆时针圆轨迹
--圆运动轨迹,参数一为动画时长,参数二为表示圆轨迹参数方程的LUA函数名,参数三为轨迹起点坐标,参数四为圆心坐标,参数五为正负圈数
self:runAction(myAction:createCircle(3.0,"CircleTraceFunc",ccp(500,300),ccp(350,300),1));
*/
class myAction : public CCActionInterval
{
public:
myAction(){memset(m_TraceFunc,0,sizeof(m_TraceFunc));m_TraceType=0;m_Func=0;m_Func1=0;m_t=0;}

/** initializes the action */
bool initWithDuration(float duration,float InitElapsed,const char *func);

virtual void startWithTarget(CCNode *pTarget);
virtual void update(float time);

public:
/** creates the action */
//相当于CC**To
static myAction* create(float duration,float InitElapsed,const char *func);
/*
带有控制参数的createXXX函数,相当于CC**By
参数说明:
centerP/rdeltArg----对于圆轨迹、摆线轨迹,分别代表圆心坐标、圆的半径和辐角主值增量
reserved----作为保留参数进一步刻画摆线轨迹,reserved.y=0时为水平向右的摆线轨迹
*/
static myAction* createCircle(float duration,float InitElapsed,const char *func,const CCPoint& startP,const CCPoint& centerP,float direction);
static myAction* createCycloid(float duration,float InitElapsed,const char *func,const CCPoint& startP,const CCPoint& rdeltArg,float direction,const CCPoint& reserved);

//add by Ivan_han 20130528
bool initWithDuration(float duration,float InitElapsed,pFuncVoid fun);
static myAction* createTrace0(float duration,float InitElapsed,pFuncTrace0 fun);
static myAction* createTrace1(float duration,float InitElapsed,pFuncTrace1 fun,const CCPoint& startP,const CCPoint& rdeltArg,float direction,const CCPoint& reserved);

//add by Ivan_han 20130531
void setFunc1(pFuncVoid Func1){m_Func1=Func1;}

//利用定义求一元实函的一阶导数,add by Ivan_han 20130608
static float dif1fx(float(*fx)(float),float x)
{
return (fx(x+0.0000001)-fx(x))/0.0000001;
}
static CCPoint dif1fx(pFuncTrace0 Func,float t)
{
float dx=(Func(t+0.0000001).x-Func(t).x);
float dy=(Func(t+0.0000001).y-Func(t).y);
return CCPoint(dx,dy);
}
protected:
char m_TraceFunc[20];//LUA函数名,定义了轨迹的参数方程
int m_TraceType;//-2表示create,-1表示createCircle和createCycloid,0表示createTrace0,1表示createTrace1
//针对圆轨迹、摆线轨迹的成员变量
CCPoint m_startP;//轨迹起点
CCPoint m_centerP;//轨迹中心点,保存参数centerP/rdeltArg
float m_direction;//1表示逆时针Anti-clockwise,-1表示顺时针Clockwise
//针对移动的圆轨迹(摆线轨迹)额外的成员变量
CCPoint m_reserved;//进一步刻画摆线轨迹

//轨迹的方程,add by Ivan_han 20130528
pFuncVoid m_Func;

//轨迹的切向量方程,add by Ivan_han 20130531
pFuncVoid m_Func1;

//上一次设置切向的时刻,m_t∈[0,1]
float m_t;
};

#include "CCActionEx.h"
#include "..\..\..\cocos2dx\support\CCPointExtension.h"

void CircleAction::startWithTarget(CCNode *pTarget){
    CCActionInterval::startWithTarget(pTarget);
    m_startPosition = pTarget->getPosition();//轨迹起点
}
 
CircleAction* CircleAction::create(float duration, const CCPoint& position,int direction){
    CircleAction * pmyAction=new CircleAction();
    pmyAction->initWithDuration(duration, position,direction);
    pmyAction->autorelease();
 
    return pmyAction;
}
//这个函数得到初始化数据
bool CircleAction::initWithDuration(float duration, const CCPoint& position,int direction){
    if (CCActionInterval::initWithDuration(duration))
    {
        m_endPosition = position; //轨迹终点,对于圆轨迹,是半圆弧轨迹的终点
  m_direction =direction;
        return true;
    }
 
    return false;
}
void CircleAction::update(float time){
    if (m_pTarget)
    {
        //这里的time就相当于当前时间占总的需要运行时间的百分比
        /*
   这里轨迹就用一个圆
   x=cos(t),y=sin(t),当时间走完就是一圈360°
   对于圆轨迹,圆心坐标C=(m_startPosition+m_endPosition)/2
        */
      CCPoint C=ccp((m_startPosition.x+m_endPosition.x)*0.5,(m_startPosition.y+m_endPosition.y)*0.5);//圆心
   float startarg=atan2(m_startPosition.y-C.y,m_startPosition.x-C.x);//轨迹起点相对于圆心的辐角主值∈(-pi,pi]
      float tempx=(m_startPosition.x-m_endPosition.x)*(m_startPosition.x-m_endPosition.x);
   float tempy=(m_startPosition.y-m_endPosition.y)*(m_startPosition.y-m_endPosition.y);
   float r=sqrt(tempx+tempy)*0.5;//圆的半径
   float x=r*cos(startarg+2*M_PI*time*m_direction);//圆上点相对于圆心的X坐标偏移
   float y=r*sin(startarg+2*M_PI*time*m_direction);//圆上点相对于圆心的Y坐标偏移
   m_pTarget->setPosition(ccpAdd(C,ccp(x,y)));
    }
}

void myAction::startWithTarget(CCNode *pTarget){
    CCActionInterval::startWithTarget(pTarget);
}

//add by Ivan_han 20130528
bool myAction::initWithDuration(float duration,float InitElapsed,pFuncVoid fun)
{
 if (CCActionInterval::initWithDuration(duration,InitElapsed))
 {
  m_Func=fun;
  return true;
 }
 return false;
}

myAction* myAction::createTrace0(float duration,float InitElapsed,pFuncTrace0 fun)
{
 myAction * pmyAction=new myAction();
 pmyAction->initWithDuration(duration,InitElapsed,(pFuncVoid)fun);
 pmyAction->m_TraceType=0;
 pmyAction->autorelease();

 return pmyAction;
}

myAction* myAction::createTrace1(float duration,float InitElapsed,pFuncTrace1 fun,const CCPoint& startP,const CCPoint& rdeltArg,float direction,const CCPoint& reserved)
{
 myAction * pmyAction=new myAction();
 if(pmyAction==NULL)
 {
  return NULL;
 }
 if(pmyAction->CCActionInterval::initWithDuration(duration,InitElapsed)==false)
 {
  return NULL;
 }
 pmyAction->m_Func=(pFuncVoid)fun;
 pmyAction->m_TraceType=1;
 pmyAction->m_startP=startP;
 pmyAction->m_centerP=rdeltArg;
 pmyAction->m_direction=direction;
 pmyAction->m_reserved=reserved;
 pmyAction->autorelease();

 return pmyAction;
}
 
myAction* myAction::create(float duration,float InitElapsed,const char *func){
    myAction * pmyAction=new myAction();
    pmyAction->initWithDuration(duration,InitElapsed,func);
 pmyAction->m_TraceType=-2;
    pmyAction->autorelease();

    return pmyAction;
}
//这个函数得到初始化数据
bool myAction::initWithDuration(float duration,float InitElapsed,const char *func){
    if (CCActionInterval::initWithDuration(duration,InitElapsed))
    {
  strcpy(m_TraceFunc,func);
        return true;
    }
    return false;
}

//带有控制参数的createXXX函数
myAction* myAction::createCircle(float duration,float InitElapsed,const char *func,const CCPoint& startP,const CCPoint& centerP,float direction)
{
    return myAction::createCycloid(duration,InitElapsed,func,startP,centerP,direction,CCPoint(0,0));
}

//带有控制参数的createXXX函数
myAction* myAction::createCycloid(float duration,float InitElapsed,const char *func,const CCPoint& startP,const CCPoint& rdeltArg,float direction,const CCPoint& reserved)
{
    myAction * pmyAction=new myAction();
    if(pmyAction==NULL)
    {
        return NULL;
    }
 if(pmyAction->CCActionInterval::initWithDuration(duration,InitElapsed)==false)
    {
        return NULL;
    }
 strcpy(pmyAction->m_TraceFunc,func);
 pmyAction->m_TraceType=-1;
 pmyAction->m_startP=startP;
 pmyAction->m_centerP=rdeltArg;
 pmyAction->m_direction=direction;
 pmyAction->m_reserved=reserved;
    pmyAction->autorelease();
 
    return pmyAction;
}

void myAction::update(float time){
    if (m_pTarget && (m_TraceFunc||m_Func))
    {
if(m_TraceType==0)
{
CCPoint retP=((pFuncTrace0)m_Func)(time);
//add by Ivan_han 20130608
if(g_pShipMgr!=NULL)
retP=g_pShipMgr->SP2CP(retP);//代表点
m_pTarget->setPosition(retP);
//针对某些任意曲线轨迹有闪烁情况
if(time-m_t<0.05 && m_t>getInitElapsed())
return;
//根据结点轨迹的切线方向更新结点的CW值
CCPoint arg1=dif1fx(((pFuncTrace0)m_Func),time);
if(g_pShipMgr!=NULL)
arg1=g_pShipMgr->SD2CD(arg1);//代表方向
float m_fArg=m_pTarget->getArg();
float angle=(m_fArg-atan2(arg1.y,arg1.x))*180/M_PI;
m_pTarget->setRotation(angle);
m_t=time;
return;
}

if(m_TraceType==1)
  {
   CCPoint retP=((pFuncTrace1)m_Func)(time,m_startP.x,m_startP.y,m_centerP.x,m_centerP.y,m_direction,m_reserved.x,m_reserved.y);
   m_pTarget->setPosition(retP);
   if(m_Func1)
   {
    //根据结点轨迹的切线方向更新结点的CW值,add by Ivan_han 20130531
    CCPoint arg1=((pFuncTrace1)m_Func1)(time,m_startP.x,m_startP.y,m_centerP.x,m_centerP.y,m_direction,m_reserved.x,m_reserved.y);
    float m_fArg=m_pTarget->getArg();//M_PI;
    float angle=(m_fArg-atan2(arg1.y,arg1.x))*180/M_PI;
    m_pTarget->setRotation(angle);
   }
   return;
  }
  if(m_TraceType==-2)
  {
    ////这里的time就相当于当前时间占总的需要运行时间的百分比
    //CCLuaEngine* pEngine = CCLuaEngine::defaultEngine();
    //lua_State* m_state = pEngine->getLuaState();
    ////int recv=pEngine->executeGlobalFunction(m_TraceFunc,1);
    ////函数名
    //lua_getglobal(m_state,m_TraceFunc);
    ////第一个参数压栈
    //lua_pushnumber(m_state, time);
    ////调用函数
    //lua_call(m_state, 1, 1);
    ////得到返回值
    //CCPoint retP=*(CCPoint*)(tolua_tousertype(m_state,2,0));
    //lua_pop(m_state, 1);

    //m_pTarget->setPosition(retP);
    return;
  }
  if(m_TraceType==-1)
  {
    ////这里的time就相当于当前时间占总的需要运行时间的百分比
    //CCLuaEngine* pEngine = CCLuaEngine::defaultEngine();
    //lua_State* m_state = pEngine->getLuaState();
    ////函数名
    //lua_getglobal(m_state,m_TraceFunc);
    ////第一个参数压栈
    //lua_pushnumber(m_state, time);
    ////第二三个参数压栈
    ////lua_pushlightuserdata(m_state, &m_startP);
    ////void *lp=lua_newuserdata(m_state,sizeof(CCPoint));
    ////memcpy(lp,&m_startP,sizeof(CCPoint));
    //lua_pushnumber(m_state,m_startP.x);
    //lua_pushnumber(m_state,m_startP.y);
    ////第四五个参数压栈
    ////lua_pushlightuserdata(m_state, &m_centerP);
    ////void *lp1=lua_newuserdata(m_state,sizeof(CCPoint));
    ////memcpy(lp1,&m_centerP,sizeof(CCPoint));
    //lua_pushnumber(m_state,m_centerP.x);
    //lua_pushnumber(m_state,m_centerP.y);
    ////第六个参数压栈
    //lua_pushnumber(m_state,m_direction);
    ////第七八个参数压栈
    //lua_pushnumber(m_state,m_reserved.x);
    //lua_pushnumber(m_state,m_reserved.y);
    ////调用函数
    //lua_call(m_state,8, 1);
    ////得到返回值
    //CCPoint retP=*(CCPoint*)(tolua_tousertype(m_state,2,0));
    //lua_pop(m_state, 1);

    ////m_pTarget->setPosition(CCPoint(m_startP.x+retP.x,m_startP.y+retP.y));//改为相对位置以支持CCRepeatForever无限次重复动画
    //m_pTarget->setPosition(retP);
    return;
  }


    }
}

 

你可能感兴趣的:(lua,cocos2d-x)