the error shows as follows:
Layer.obj : error LNK2001: unresolved external symbol “public: virtual void __thiscall Layer01::onKeyPressed(enum cocos2d::EventKeyboard::KeyCode,class cocos2d::Event *)” (?onKeyPressed@Layer01@@UAEXW4KeyCode@EventKeyboard@cocos2d@@PAVEvent@4@@Z)
2>Layer.obj : error LNK2001: unresolved external symbol “public: virtual void __thiscall Layer01::onKeyReleased(enum cocos2d::EventKeyboard::KeyCode,class cocos2d::Event *)” (?onKeyReleased@Layer01@@UAEXW4KeyCode@EventKeyboard@cocos2d@@PAVEvent@4@@Z)
post your cpp/.h?
The Layer.h as follows:
#ifndef __Layer_H__
#define __Layer_H__
#include "cocos2d.h"
using namespace cocos2d;
class Layer01 : public cocos2d::Layer
{
private:
std::map<cocos2d::EventKeyboard::KeyCode, bool> keys;
EventListenerKeyboard* listener;
EventDispatcher* _eventDispatcher;
cocos2d::Sprite* node2;
public:
static cocos2d::Layer* createLayer();
virtual bool init();
void menuCloseCallback(cocos2d::Ref* pSender);
Layer01::Layer01(){
listener=EventListenerKeyboard::create();
_eventDispatcher = _director->getEventDispatcher();
node2=nullptr;
}
CREATE_FUNC(Layer01);
void onKeyPressed(cocos2d::EventKeyboard::KeyCode keyCode,cocos2d::Event* event);
void onKeyReleased(cocos2d::EventKeyboard::KeyCode keyCode,cocos2d::Event* event);
};
#endif // __HELLOWORLD_SCENE_H__
the Layer.cpp as follows:
#include "HelloWorldScene.h"
#include "SimpleAudioEngine.h"
#include "Layer.h"
#include "cocos2d.h"
#include<cmath>
#include<stdlib.h>
#include "AppDelegate.h"
using namespace std;
using namespace cocos2d;
using namespace CocosDenshion;
USING_NS_CC;
Layer* Layer01::createLayer()
{
return Layer01::create();
}
// Print useful error message instead of segfaulting when files are not there.
static void problemLoading(const char* filename)
{
printf("Error while loading: %s\n", filename);
printf("Depending on how you compiled you might have to add 'Resources/' in front of filenames in HelloWorldScene.cpp\n");
}
// on "init" you need to initialize your instance
bool Layer01::init()
{
// 1. super init first
if (!Layer::init())
{
return false;
}
auto visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();
auto closeItem = MenuItemImage::create(
"CloseNormal.png",
"CloseSelected.png",
CC_CALLBACK_1(Layer01::menuCloseCallback, this));
if (closeItem == nullptr ||
closeItem->getContentSize().width <= 0 ||
closeItem->getContentSize().height <= 0)
{
problemLoading("'CloseNormal.png' and 'CloseSelected.png'");
}
else
{
float x = origin.x + visibleSize.width - closeItem->getContentSize().width / 2;
float y = origin.y + closeItem->getContentSize().height / 2;
closeItem->setPosition(Vec2(x, y));
}
// create menu, it's an autorelease object
auto menu = Menu::create(closeItem, NULL);
menu->setPosition(Vec2::ZERO);
this->addChild(menu, 1);
auto label = Label::createWithTTF("Hello World", "fonts/Marker Felt.ttf", 24);
if (label == nullptr)
{
problemLoading("'fonts/Marker Felt.ttf'");
}
else
{
// 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");
if (sprite == nullptr)
{
problemLoading("'HelloWorld.png'");
}
else
{
// 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);
}
node2=Sprite::create("ty.png");
node2->setPosition(Vec2(770,160));
this->addChild(node2);
auto listener=EventListenerKeyboard::create();
listener->onKeyPressed = [=](EventKeyboard::KeyCode keyCode,Event* event){
keys[keyCode] = true;
};
listener->onKeyReleased = [=](EventKeyboard::KeyCode keyCode,Event* event){
keys[keyCode] = false;
};
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener,this);
return true;
}
void Layer01::menuCloseCallback(cocos2d::Ref* pSender){
}
the demo file as follows:
demo.zip (1.2 MB)
You have no implememtations for onKeyReleased and onKeyPressed. The linker error occurs by the reason. You can check corresponding examples from cocos test cpp project about using these callbacks
Or remove them if they are extra in your case
1 Like
This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.