标签:cat pat show 移动 listen roo location idt pclose
#include "HelloWorldScene.h"
#include "SimpleAudioEngine.h"
#include "MyHelloWorldScene.h"
USING_NS_CC;
Scene* HelloWorld::createScene()
{
// ‘scene‘ is an autorelease object
auto scene = Scene::create();
// ‘layer‘ is an autorelease object
auto layer = HelloWorld::create();
// add layer as a child to scene
scene->addChild(layer);
// return the scene
return scene;
}
void HelloWorld::backHome() {
Size visibleSize = Director::getInstance()->getVisibleSize();
Label* lable = Label::create("I am home!", "Arial", 35);
lable->setPosition(Point(visibleSize.width / 2, visibleSize.height / 2));
this->addChild(lable);
}
// on "init" you need to initialize your instance
bool HelloWorld::init()
{
//////////////////////////////
// 1. super init first
if (!Layer::init())
{
return false;
}
//多点触摸 Chapter5_4_AllOnceTouchEvent
//TODO
// 单点触摸,判断触摸了精灵如按钮点击效果
Size visibleSize = Director::getInstance()->getVisibleSize();
Sprite* sp1 = Sprite::create("sprite1.png");
sp1->setPosition(Point(visibleSize.width / 2, visibleSize.height / 2));
this->addChild(sp1);
Sprite* sp2 = Sprite::create("sprite2.png");
sp2->setPosition(Point(visibleSize.width / 2, visibleSize.height / 2));
this->addChild(sp2);
auto listener = EventListenerTouchOneByOne::create();
listener->onTouchBegan = [](Touch* touch, Event* event) {
//注册监听事件时绑定了一个Node对象,在这里就可以取出这个对象
auto target = static_cast<Sprite*>(event->getCurrentTarget());
Point pos = Director::getInstance()->convertToGL(touch->getLocationInView());
//判断单击的坐标是否在精灵的范围内
if (target->getBoundingBox().containsPoint(pos)) {
//设置精灵的透明度100
target->setOpacity(100);
return true;
}
return false;
};
listener->onTouchEnded = [](Touch* touch, Event* event) {
//恢复精灵的透明度
auto target = static_cast<Sprite*>(event->getCurrentTarget());
target->setOpacity(255);
};
//注册监听事件,绑定精灵1
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, sp1);
//注册监听事件,绑定精灵2,这里要注意,listener对象复制了一份
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener->clone(), sp2);
return true;
//// TouchEvent 触摸事件 onTouchBegan触摸事件开始、onTouchMoved触摸移动事件、onTouchEnded触摸事件结束、onTouchCancelled打断事件
//auto listener = EventListenerTouchOneByOne::create();
//listener->onTouchBegan = [](Touch* touch, Event* event) {
// Point pos1 = touch->getLocation();//获取单击坐标,基于3D
// Point pos2 = touch->getLocationInView();//获取单击坐标,基于2D
// Point pos3 = Director::getInstance()->convertToGL(pos2);//获取单击坐标,基于Cocos2d-x
// log("HelloWorldScene onTouchBegan! pos1 x=%f,y=%f", pos1.x, pos1.y);
// log("HelloWorldScene onTouchBegan! pos2 x=%f,y=%f", pos2.x, pos2.y);
// log("HelloWorldScene onTouchBegan! pos3 x=%f,y=%f", pos3.x, pos3.y);
// return true;
//};
//listener->onTouchMoved = [](Touch* touch, Event* event) {
// log("HelloWorldScene onTouchMoved");
//};
//listener->onTouchEnded = [=](Touch* touch, Event* event) {
// log("HelloWorldScene onTouchEnded");
//};
//_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
//return true;
//// 动作结束监听callbackFunc C++的功能
//Size visibleSize = Director::getInstance()->getVisibleSize();
//Sprite* xiaoRuo = Sprite::create("2.png");
//xiaoRuo->setPosition(Point(visibleSize.width / 2, visibleSize.height / 2));
//this->addChild(xiaoRuo);
////移动动作
//MoveTo* moveToHome = MoveTo::create(10.0f, Point(visibleSize.width, visibleSize.height / 2));
////回调对象,CallFunc也是一个动作,只不过这个动作是回调一个函数
//auto callbackFunc = [&]() {backHome(); };
//CallFunc* callFunc = CallFunc::create(callbackFunc);
////组合两个动作
//Action* actions = Sequence::create(moveToHome, callbackFunc, NULL);
////执行动作,小若开始回家
//xiaoRuo->runAction(actions);
//return true;
//// Sequence多个动作一个一个播放、Spawn多个动作一起播放
//Size visibleSize = Director::getInstance()->getVisibleSize();
//Sprite* xiaoRuo = Sprite::create("2.png");
//xiaoRuo->setPosition(Point(visibleSize.width / 2, visibleSize.height / 2));
//this->addChild(xiaoRuo);
////创建一个移动对象
//MoveBy* moveBy = MoveBy::create(2.2f, Point(40, 20));
////创建一个弹跳动作对象,弹跳高度为100,弹跳次数为5
//JumpBy* jumpBy = JumpBy::create(3.0f, Point(50, 1), 100, 5);
////创建一个旋转动作对象
//RotateBy* rotateBy = RotateBy::create(2.5f, 220, 10);
////创建组合动作对象,将所有动作连起来
////Action* actions = Spawn::create(moveBy, jumpBy, rotateBy, NULL);
//Action* actions = Sequence::create(moveBy, jumpBy, rotateBy, NULL);
//xiaoRuo->runAction(actions);
//return true;
//// RepeatForver循环动作
//Size visibleSize = Director::getInstance()->getVisibleSize();
//Sprite* xiaoRuo = Sprite::create("2.png");
//xiaoRuo->setPosition(Point(visibleSize.width / 2, visibleSize.height / 2));
//this->addChild(xiaoRuo);
////创建一个JumpBy动作对象
//JumpBy* jumpBy = JumpBy::create(3.0f, Point(50, 1), 100, 1);
////以jumpBy为参数,创建一个永久性的重复动作
//RepeatForever* repeatForverAction = RepeatForever::create(jumpBy);
////以jumpBy为参数,创建一个重复次数的动作
//Repeat* repeatAction=Repeat::create(jumpBy,3);
////执行动作
//xiaoRuo->runAction(repeatForverAction);
//return true;
//Blink 精灵闪烁
//Size visibleSize = Director::getInstance()->getVisibleSize();
//Sprite* xiaoRuo = Sprite::create("2.png");
//xiaoRuo->setPosition(Point(visibleSize.width / 2, visibleSize.height / 2));
//this->addChild(xiaoRuo);
////创建Blink动作对象
//Blink* blink = Blink::create(3.0f, 3);//持续时间、闪烁次数
//xiaoRuo->runAction(blink);
//return true;
//// ScaleTo和ScaleBy缩放精灵
////创建一个精灵
//Size visibleSize = Director::getInstance()->getVisibleSize();
//Sprite* xiaoRuo = Sprite::create("2.png");
//xiaoRuo->setPosition(Point(visibleSize.width/2,visibleSize.height/2));
//this->addChild(xiaoRuo);
//创建MoveTo动作对象
//ScaleTo* scaleTo = ScaleTo::create(2.8f,0.4f,1.0f);//参数1:持续时间 2:X方向的拉伸值 3:Y方向的拉伸值
//ScaleBy* scaleTo = ScaleBy::create(2.8f, 0.4f, 1.0f);//参数1:持续时间 2:X方向的拉伸值 3:Y方向的拉伸值
//xiaoRuo->runAction(scaleTo);
//return true;
//// MoveTo移动到指定坐标、MoveBy移动距离,移动精灵
////创建一个精灵
//Size visibleSize = Director::getInstance()->getVisibleSize();
//Sprite* xiaoRuo = Sprite::create("2.png");
//xiaoRuo->setPosition(Point(50, visibleSize.height / 2));
//this->addChild(xiaoRuo);
////创建MoveTo动作对象
////MoveTo* moveTo = MoveTo::create(0.9f, Point(250, 150));
//MoveBy* moveTo = MoveBy::create(0.9f, Point(250, 150));//0.9f动作持续时间
////精灵执行动作
//xiaoRuo->runAction(moveTo);
//return true;
//// 九妹,可伸缩的图片,九妹结合按钮制作按钮的点击效果
////Scale9Sprite* nineGirl=Scale9Sprite::create
////播放特效音乐
//CocosDenshion::SimpleAudioEngine::getInstance()->playEffect("effect1.wav");
//return true;
////播放背景音乐
//CocosDenshion::SimpleAudioEngine::getInstance()->playBackgroundMusic("background.mp3", true);
//return true;
//Vector是cocos带的队列,Map是cocos带的map
//Vector<String> vector;
//vector.pushBack("a");
//vector.pushBack("b");
//for (auto a :vector)
//{
// log("%s", a);
//}
//Map<int, Label*> map;
//return true;
//Value可以格式化字符
//Value valStr = Value("XiaoRuo is");
//Value valInt = Value(250);
//log("%s%d", valStr.asString().c_str(), valInt.asInt());
//return true;
//创建菜单MenuItemImage MenuItemLabel并且设置排列方式
//Size visibleSize = Director::getInstance()->getVisibleSize();
//MenuItemImage* pCloseItem = MenuItemImage::create(
// "CloseNormal.png", "CloseSelected.png", this, menu_selector(HelloWorld::menuCloseCallback)
//);
//Label* label = Label::create("I am Label Item.", "Arial", 30);
//MenuItemLabel* pLabelItem = MenuItemLabel::create(label);
//Menu* pmenu = Menu::create(pCloseItem, pLabelItem, NULL);
//pmenu->alignItemsVertically();
//pmenu->setPosition(Point(visibleSize.width / 2, visibleSize.height / 2));
//this->addChild(pmenu, 1);
//return true;
//创建精灵
Sprite* sprite = Sprite::create("mypic.png");
sprite->setPosition(Point(200, 200));
this->addChild(sprite);
return true;
//auto visibleSize = Director::getInstance()->getVisibleSize();
//Vec2 origin = Director::getInstance()->getVisibleOrigin();
///////////////////////////////
//// 2. add a menu item with "X" image, which is clicked to quit the program
//// you may modify it.
//// add a "close" icon to exit the progress. it‘s an autorelease object
//auto closeItem = MenuItemImage::create(
// "CloseNormal.png",
// "CloseSelected.png",
// CC_CALLBACK_1(HelloWorld::menuCloseCallback, this));
//closeItem->setPosition(Vec2(origin.x + visibleSize.width - closeItem->getContentSize().width / 2,
// origin.y + closeItem->getContentSize().height / 2));
//// create menu, it‘s an autorelease object
//auto menu = Menu::create(closeItem, NULL);
//menu->setPosition(Vec2::ZERO);
//this->addChild(menu, 1);
///////////////////////////////
//// 3. add your codes below...
//// add a label shows "Hello World"
//// create and initialize a label
//auto label = Label::createWithTTF("Hello World", "fonts/Marker Felt.ttf", 24);
//// position the label on the center of the screen
//label->setPosition(Vec2(origin.x + visibleSize.width / 2,
// origin.y + visibleSize.height - label->getContentSize().height));
//// add the label as a child to this layer
//this->addChild(label, 1);
//// add "HelloWorld" splash screen"
//auto sprite = Sprite::create("HelloWorld.png");
//// position the sprite on the center of the screen
//sprite->setPosition(Vec2(visibleSize.width / 2 + origin.x, visibleSize.height / 2 + origin.y));
//// add the sprite as a child to this layer
//this->addChild(sprite, 0);
//return true;
}
void HelloWorld::menuCloseCallback(Ref* pSender)
{
//Director::getInstance()->replaceScene(MyHelloWorld::createScene());
/*Director::getInstance()->replaceScene(TransitionSlideInT::create(3.0f, MyHelloWorld::createScene()));*/
Director::getInstance()->pushScene(TransitionSlideInT::create(3.0f, MyHelloWorld::createScene()));//不释放场景
////Close the cocos2d-x game scene and quit the application
//Director::getInstance()->end();
//#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
//exit(0);
//#endif
/*To navigate back to native iOS screen(if present) without quitting the application ,do not use Director::getInstance()->end() and exit(0) as given above,instead trigger a custom event created in RootViewController.mm as below*/
//EventCustom customEndEvent("game_scene_close_event");
//_eventDispatcher->dispatchEvent(&customEndEvent);
}
标签:cat pat show 移动 listen roo location idt pclose
原文地址:http://www.cnblogs.com/zhuawang/p/6493352.html