//
// GameScence.hpp
// NotesDamo
//
// Created by apple on 16/10/23.
//
// #ifndef GameScence_hpp
#define GameScence_hpp #include <stdio.h>
#include "cocos2d.h" class GameScence : public cocos2d::Layer
{
private:
//创建一个私有的精灵成员变量
cocos2d::Sprite * m_spriteGun;
public: static cocos2d::Scene* createScene();//声明创建当前的层 virtual bool init();//声明初始化层实例函数。 bool onTouchBegan(cocos2d::Touch* touch, cocos2d::Event* event);//手势点击事件 CREATE_FUNC(GameScence);//CREATE_FUNC是cocos2d-x中定义的一个宏(作用是:创建一个静态函数"static create()",该函数可以用来创建层); };
#endif /* GameScence_hpp */
//
// GameScence.cpp
// NotesDamo
//
// Created by apple on 16/10/23.
//
// #include "GameScence.hpp" USING_NS_CC; Scene* GameScence::createScene()
{
auto scene = Scene::create(); auto layer = GameScence::create(); scene->addChild(layer); return scene;
} GameScence::GameScence()
{ }
bool GameScence::init()
{
//////////////////////////////
// 1. super init first
// 初始化父类
if ( !Layer::init() )
{
return false;
} m_spriteGun = Sprite::create(StringUtils::format("gun2_0.png"));
m_spriteGun->setPosition(, );
this ->addChild(m_spriteGun); //声明
auto listener = EventListenerTouchOneByOne::create();
listener->setSwallowTouches(true); //注册事件
listener->onTouchBegan = CC_CALLBACK_2(GameScence::onTouchBegan, this); _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); return true;
} bool GameScence::onTouchBegan(cocos2d::Touch* touch, cocos2d::Event* event)
{ touch->getLocation();// 获取当前点击的坐标 m_spriteGun->cocos2d::Node::setRotation(atan2((touch->getLocation().x-m_spriteGun->getPositionX()),(touch->getLocation().y-m_spriteGun->getPositionY()))*/3.1415926);//改变弧度 后面加不加90要根据精灵的初始角度是怎样的 return true;
}