怎么复制cocos2d键盘控制精灵的精灵,或者给精灵截图

cocos2d-x中如何实现点击一下精灵让精灵瞬间变成另一张精灵
1、在.h文件中
void imageChange(CCObject *obj);
void imageChange2(CCObject *obj);
void addButton(const char * str,const char * str2 ,CCPoint xy
,SEL_MenuHandler ffName,bool vis,int z,int tag);
2、在.cpp文件中
在OnEnterFinish函数中
CCLog("liiiiiiiiiiiii 快速回复3");
if(StateDirectory::sharedGame()-&sg-&sg_fight_is_kshf==1){
this-&addButton("kshf1.png","kshf2.png"
,ccp(55,25),menu_selector(Battle::imageChange),true
CCLog("liiiiiiiiiiiii 快速回复4");
if(StateDirectory::sharedGame()-&sg-&sg_fight_teamid & 0
StateDirectory::sharedGame()-&sg-&sg_fight_is_jxzd==1){
CCLog("liiiiiiiiiiiii 继续战斗3");
this-&addButton("jxzd1.png","jxzd2.png",ccp(267,25),menu_selector(Battle::imageChange2),true
CCLog("liiiiiiiiiiiii 继续战斗4");
void Battle::imageChange(CCObject *obj){
StateDirectory::sharedGame()-&sg-&sg_MouseDOWN(101,0);
void Battle::imageChange2(CCObject *obj){
StateDirectory::sharedGame()-&sg-&sg_MouseDOWN(102,0);
void Battle::addButton(const char * str,const char * str2 ,CCPoint
xy ,SEL_MenuHandler ffName,bool vis,int z,int tag)
CCMenuItemImage *temp = CCMenuItemImage::itemFromNormalImage(str
,str2,this,ffName);//这里的nsselector...是把字符转化为SEL
&&& CCMenu
*tempButton = CCMenu::menuWithItems(temp,NULL);
tempButton-&setPosition(xy);
tempButton-&setIsVisible(vis);
&&& this-&
addChild(tempButton ,z,tag);
已投稿到:
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。Cocos2d-x中,如何通过触摸来移动一个精灵
Cocos2d-x中,如何通过触摸来移动一个精灵
Cocos2d-x中,如何通过按住一个精灵移动手指,使精灵跟随手指移动,方法如下:
首先添加精灵,定义触摸事件
bool HelloWorld::init()
if ( !Layer::init() )
ball = Sprite::create(&Ball.png&);
auto listener = EventListenerTouchOneByOne::create();
listener-&onTouchMoved = CC_CALLBACK_2(HelloWorld::onTouchMoved, this);
listener-&onTouchBegan = CC_CALLBACK_2(HelloWorld::onTouchBegin, this);
listener-&onTouchEnded = CC_CALLBACK_2(HelloWorld::onTouchEnd, this);
_eventDispatcher-&addEventListenerWithSceneGraphPriority(listener, this);
addChild(ball);
}然后创建onTouchMoved函数
void HelloWorld::onTouchMoved(Touch* touch, Event* event){
Rect ballRect = ball-&getBoundingBox();
//获取精灵的区域
Vec2 beginPoint = touch-&getLocation();
//获取触摸的开始位置
if (ballRect.containsPoint(beginPoint)){
Point endPoint = touch-&getPreviousLocation();
//获取触摸的结束位置
Point offSet = beginPoint - endP
//计算出两个位置的差
Point newPosition = ball-&getPosition() + offS
//计算出精灵现在应该在的位置
ball-&setPosition(newPosition);
//把精灵的位置设置到它应该在的位置
}这样即可实现基本的精灵跟随触摸位置移动。
不过这个方法有一定的问题,当快速移动时,精灵可能会丢失触摸的位置,需要重新点击并且移动才可以继续跟上手指。
我的热门文章
即使是一小步也想与你分享Cocos2D-JS 动作回调取得当前动作精灵 - 简书
Cocos2D-JS 动作回调取得当前动作精灵
今天碰到一个需求,要在动作结束的回调函数中取到当前动作的精灵,实现在执行动作之后自动 remove 掉这个精灵。查了一下,没有结果。然后看了一下 API ,发现利用 cc.CallFunc 构造函数中的 data 参数可以解决这个问题。下面是 cc.CallFunc 的构造函数和 init 方法的实现:
ctor:function(selector, selectorTarget, data){
cc.FiniteTimeAction.prototype.ctor.call(this);
if(selector !== undefined){
if(selectorTarget === undefined)
this.initWithFunction(selector);
else this.initWithFunction(selector, selectorTarget, data);
initWithFunction:function (selector, selectorTarget, data) {
if (selectorTarget) {
this._data =
this._callFunc =
this._selectorTarget = selectorT
else if (selector)
this._function =
selector 和 selectorTarget 不赘述。注意到在 init 方法中,将 ctor 方法传入的 data 赋给了 this 的 _data 。再看它的 execute 方法 :
execute:function () {
if (this._callFunc != null)
//CallFunc, N, ND
this._callFunc.call(this._selectorTarget, this.target, this._data);
else if(this._function)
this._function.call(null, this.target);
在执行这个回调的时候将 this._data 传入了原方法中,这样我们就可以将当前动作精灵指针传入回调函数,并在回调中取得它。
先构造一个动作队列并运行,代码如下:
var actionSprite = new cc.Sprite("res/me.png");
actionSprite.setAnchorPoint(0, 0);
actionSprite.x = 0;
actionSprite.y = 0;
this.addChild(actionSprite, 1);
var moveAction0 = cc.MoveBy(0.5, cc.p(cc.winSize.width/2, 0));
var moveAction1 = cc.MoveBy(0.5, cc.p(0, cc.winSize.height/2));
var moveAction2 = cc.MoveBy(0.5, cc.p(-cc.winSize.width/2, 0));
var moveAction3 = cc.MoveBy(0.5, cc.p(0, -cc.winSize.height/2));
var actionCallbackFunction = cc.CallFunc(this.actionCallbackFunction, this);
var actionFinishCallbackFunction = cc.CallFunc(this.actionFinishCallFunction, this);
var actionArray = [];
actionArray.push(moveAction0);
actionArray.push(actionCallbackFunction);
actionArray.push(moveAction1);
actionArray.push(actionCallbackFunction);
actionArray.push(moveAction2);
actionArray.push(actionCallbackFunction);
actionArray.push(moveAction3);
actionArray.push(actionFinishCallbackFunction);
var actionSequence = cc.Sequence(actionArray);
actionSprite.runAction(actionSequence);
actionCallbackFunction : function(){
cc.log("Action Callback Function");
actionFinishCallFunction : function(){
cc.log("Action Finish Callback Function");
下面对以上代码做一下小修改,来取得执行动作的精灵。首先我们在构造回调函数时要将精灵的指针作为 data 参数传入:
var actionCallbackFunction = cc.CallFunc(this.actionCallbackFunction, this, actionSprite);
var actionFinishCallbackFunction = cc.CallFunc(this.actionFinishCallFunction, this, actionSprite);
其次我们要在定义回调方法的参数表中加入 data ,以便在函数实现中使用它:
actionCallbackFunction : function(data){
cc.log("Action Callback Function");
if (data != null && data != "undefine"){
cc.log("Get Data");
cc.log(typeof data);
cc.log(data.x);
cc.log(data.y);
actionFinishCallFunction : function(data){
cc.log("Action Finish Callback Function");
if (data != null && data != "undefine"){
cc.log("Get Data");
cc.log(typeof data);
cc.log(data.x);
cc.log(data.y);
data.removeFromParent();
运行试试看~
控制台这一部分输出为:
JS: Action Callback Function
JS: Get Data
JS: object
JS: Action Callback Function
JS: Get Data
JS: object
JS: Action Callback Function
JS: Get Data
JS: object
JS: Action Finish Callback Function
JS: Get Data
JS: object
看到在每一次动作执行完毕后输出 data 类型为 object 和精灵当前的坐标,最后的 removeFromParent() 也成功了。
接下来试一下来构造一个函数,返回一个动作队列。这样,只要创建动作队列然后在精灵上 runAction 就自动实现了精灵动作并在动作结束之后自动清掉这个精灵。创建动作队列的代码如下:
createActionSequence : function(actionSpritePointer){
var moveAction0 = cc.MoveBy(0.5, cc.p(cc.winSize.width/2, 0));
var moveAction1 = cc.MoveBy(0.5, cc.p(0, cc.winSize.height/2));
var moveAction2 = cc.MoveBy(0.5, cc.p(-cc.winSize.width/2, 0));
var moveAction3 = cc.MoveBy(0.5, cc.p(0, -cc.winSize.height/2));
var actionCallbackFunction = cc.CallFunc(this.actionCallbackFunction, this, actionSpritePointer);
var actionFinishCallbackFunction = cc.CallFunc(this.actionFinishCallFunction, this, actionSpritePointer);
var actionArray = [];
actionArray.push(moveAction0);
actionArray.push(actionCallbackFunction);
actionArray.push(moveAction1);
actionArray.push(actionCallbackFunction);
actionArray.push(moveAction2);
actionArray.push(actionCallbackFunction);
actionArray.push(moveAction3);
actionArray.push(actionFinishCallbackFunction);
var actionSequence = cc.Sequence(actionArray);
return actionS
参数 actionSpritePointer 要在创建时传入要执行这个动作队列的精灵,这样,就能把这个精灵的指针传入回调函数中。创建动作队列并执行的代码如下:
var actionSprite0 = new cc.Sprite("res/me.png");
var actionSprite1 = new cc.Sprite("res/me.png");
actionSprite0.setAnchorPoint(0, 0);
actionSprite1.setAnchorPoint(1, 0);
actionSprite0.x = 0;
actionSprite0.y = 0;
actionSprite1.x = cc.winSize.width/2;
actionSprite1.y = 0;
this.addChild(actionSprite0);
this.addChild(actionSprite1);
var actionSequence0 = this.createActionSequence(actionSprite0);
var actionSequence1 = this.createActionSequence(actionSprite1);
cc.log(typeof actionSequence0);
actionSprite0.runAction(actionSequence0);
actionSprite1.runAction(actionSequence1);
谢谢观赏,如果有错误,欢迎指正~

我要回帖

更多关于 cocos2dx 复制精灵 的文章

 

随机推荐