I'm kinda new to C++ however, this was a code that was provided to me and I was told to do some edits but very few. I Keep getting the error for two lines of code:
MyFrame() : wxFrame((wxFrame *)NULL, -1, wxT("wxTimerDemo " + wxDataTime::Now().Format("%c"))
, wxPoint(50,50), wxSize(500,300))
and
dc.DrawText(wxT("Testing"), 40, y);
The top one is the only code edited which was followed exactly as it should've.
Full code:
// Render timer - use of a timer
// http://wiki.wxwidgets.org/Making_a_render_loop
#include <wx/sizer.h>
#include <wx/wx.h>
#include <wx/timer.h>
// prototypes
class BasicDrawPane;
class MyFrame;
// class definitions
class RenderTimer : public wxTimer
{
BasicDrawPane* pane;
public:
RenderTimer(BasicDrawPane* pane);
void Notify();
void start();
};
#define wxT(x)
class BasicDrawPane : public wxPanel
{
public:
BasicDrawPane(wxFrame* parent);
void paintEvent(wxPaintEvent& evt);
void render( wxDC& dc );
DECLARE_EVENT_TABLE()
};
class MyApp: public wxApp
{
bool OnInit();
MyFrame* frame;
public:
};
RenderTimer::RenderTimer(BasicDrawPane* pane) : wxTimer()
{
RenderTimer::pane = pane;
}
void RenderTimer::Notify()
{
pane->Refresh();
}
void RenderTimer::start()
{
wxTimer::Start(10);
}
IMPLEMENT_APP(MyApp)
class MyFrame : public wxFrame
{
RenderTimer* timer;
BasicDrawPane* drawPane;
public:
MyFrame() : wxFrame((wxFrame *)NULL, -1, wxT("wxTimerDemo " + wxDataTime::Now().Format("%c"))
, wxPoint(50,50), wxSize(500,300))
{
drawPane = new BasicDrawPane( this );
timer = new RenderTimer(drawPane);
Show();
timer->start();
}
~MyFrame()
{
delete timer;
}
void onClose(wxCloseEvent& evt)
{
timer->Stop();
evt.Skip();
}
DECLARE_EVENT_TABLE()
};
// event table for MyFrame
BEGIN_EVENT_TABLE(MyFrame, wxFrame)
EVT_CLOSE(MyFrame::onClose)
END_EVENT_TABLE()
bool MyApp::OnInit()
{
frame = new MyFrame();
frame->Show();
return true;
}
// event table for BasicDrawPane
BEGIN_EVENT_TABLE(BasicDrawPane, wxPanel)
EVT_PAINT(BasicDrawPane::paintEvent)
END_EVENT_TABLE()
BasicDrawPane::BasicDrawPane(wxFrame* parent) :
wxPanel(parent)
{
}
void BasicDrawPane::paintEvent(wxPaintEvent& evt)
{
wxPaintDC dc(this);
render(dc);
}
void BasicDrawPane::render( wxDC& dc )
{
static int y = 0;
static int y_speed = 2;
y += y_speed;
if(y<0) y_speed = 2;
if(y>200) y_speed = -2;
dc.SetBackground( *wxWHITE_BRUSH );
dc.Clear();
dc.DrawText(wxT("Testing"), 40, y);
}
Thanks for any help.
#define wxT(x) transforms any x to blank.
dc.DrawText(wxT("Testing"), 40, y); becomes dc.DrawText( , 40, y); and now the error is obvious.
You should define the macro like
#define wxT(x) x
or
#define wxT(x) (x)
Defining the macro wxT is not a good use case, it should be defined when you #include <wx/string.h>, thus #define wxT(x) should be removed from your code. For more information visit #define wxT ( string ). Note that since wxWidgets 2.9.0 you shouldn't use wxT() anymore in your program sources (it was previously required if you wanted to support Unicode).
Related
#include "wx/wx.h"
class MyFrame : public wxFrame{
public:
MyFrame();
~MyFrame();
private:
//DECLARE_EVENT_TABLE()
};
class MyWindow : public wxWindow{
public:
void OnPaint(wxPaintEvent& event);
private:
DECLARE_EVENT_TABLE()
};
class MyApp : public wxApp
{
public:
MyApp();
~MyApp();
virtual bool OnInit();
void DrawSimpleShapes(wxDC& dc);
private:
MyFrame* m_frame = NULL;
//MyWindow* w = NULL;
};
MyFrame::MyFrame() : wxFrame(nullptr,wxID_ANY,"Rectangle",wxPoint(30,30),wxSize(800,600))
{
}
bool MyApp :: OnInit()
{
m_frame = new MyFrame();
m_frame->Show();
//w = new MyWindow();
//w->Show();
return true;
}
wxIMPLEMENT_APP(MyApp);
wxBEGIN_EVENT_TABLE(MyWindow,wxWindow)
EVT_PAINT(MyWindow::OnPaint)
wxEND_EVENT_TABLE()
MyFrame::~MyFrame()
{
}
MyApp::MyApp()
{
}
MyApp::~MyApp()
{
}
void MyWindow :: OnPaint(wxPaintEvent& event)
{
wxPaintDC dc(this);
dc.SetPen(*wxBLACK_PEN);
dc.SetBrush(*wxRED_BRUSH);
wxSize sz = GetClientSize();
wxCoord w = 100, h = 50;
int x = wxMax(0,(sz.x-w)/2);
int y = wxMax(0,(sz.y - h)/2);
wxRect recToDraw(x,y,w,h);
dc.DrawRectangle(recToDraw);
}
I need some guidance learning wxWidgets. What's the problem with my code? When I run this code it does not print any rectangle. Instead it just print window only. I am new to wxWidgets library so it is difficult for me to find any errors. I cannot do any error handling in the wxWidgets.
There are multiple issues with the code posted, but I'll restrict this answer to the question that was asked. If you want to draw a rectangle on the applications frame, you need to
declare the OnPaint method in the frame class, and
alter the event table macro to set the OnPaint method to handle the paint event.
Here is a corrected example with these 2 changes:
#include "wx/wx.h"
class MyFrame : public wxFrame{
public:
MyFrame();
~MyFrame();
private:
void OnPaint(wxPaintEvent& event);
DECLARE_EVENT_TABLE()
};
class MyApp : public wxApp
{
public:
MyApp();
~MyApp();
virtual bool OnInit();
private:
MyFrame* m_frame = NULL;
};
MyFrame::MyFrame() : wxFrame(nullptr,wxID_ANY,"Rectangle",wxPoint(30,30),wxSize(800,600))
{
}
bool MyApp :: OnInit()
{
m_frame = new MyFrame();
m_frame->Show();
return true;
}
wxIMPLEMENT_APP(MyApp);
wxBEGIN_EVENT_TABLE(MyFrame,wxFrame)
EVT_PAINT(MyFrame::OnPaint)
wxEND_EVENT_TABLE()
MyFrame::~MyFrame()
{
}
MyApp::MyApp()
{
}
MyApp::~MyApp()
{
}
void MyFrame :: OnPaint(wxPaintEvent& event)
{
wxPaintDC dc(this);
dc.SetPen(*wxBLACK_PEN);
dc.SetBrush(*wxRED_BRUSH);
wxSize sz = GetClientSize();
wxCoord w = 100, h = 50;
int x = wxMax(0,(sz.x-w)/2);
int y = wxMax(0,(sz.y - h)/2);
wxRect recToDraw(x,y,w,h);
dc.DrawRectangle(recToDraw);
}
In the code you posted, you had an extra MyWindow class, but that class was never used anywhere.
I'm doing a school project and I'm stuck here. I'm trying to make my hexagon gradually change its color from yellow to blue. This is my hexagon.hh:
#ifndef HEXAGON_HH
#define HEXAGON_HH
#include <QGraphicsPolygonItem>
#include <QPropertyAnimation>
#include "gamecontroller.hh"
class GameController;
class Hexagon : public QGraphicsPolygonItem
{
public:
Hexagon(QGraphicsItem *parent = 0);
~Hexagon();
GameController* _controller;
Common::CubeCoordinate _coord;
protected:
void mousePressEvent(QGraphicsSceneMouseEvent* event);
};
#endif // HEXAGON_HH
I tried to use QPropertyAnimation like this:
QPropertyAnimation* animation = new QPropertyAnimation(_Tilemap.at(tileCoord)->hexagon_, "brush");
animation->setDuration(10000);
animation->setStartValue(QBrush(Qt::yellow()));
animation->setEndValue(QBrush(Qt::blue()));
animation->start();
But it couldn't be used on the hexagon class so it didn't work. How could I make the hexagon change its color so that there's an animation?
e: heres the error I get when I tried to use QPropertyAnimation:
/home/litmanen/test/UI/gamecontroller.cpp:256: error: no matching function for call to ?QPropertyAnimation::QPropertyAnimation(Hexagon*&, const char [6])?
QPropertyAnimation* animation = new QPropertyAnimation(_Tilemap.at(tileCoord)->hexagon_, "brush");
The error is caused because QPropertyAnimation only apply to QObject, in your case QGraphicsPolygonItem is not. So a possible solution is to inherit from QObject:
*.h
#ifndef HEXAGON_H
#define HEXAGON_H
#include <QBrush>
#include <QGraphicsPolygonItem>
#include <QObject>
class Hexagon : public QObject, public QGraphicsPolygonItem
{
Q_OBJECT
Q_PROPERTY(QBrush brush READ brush WRITE setBrush)
public:
explicit Hexagon(QObject *parent=nullptr);
GameController* _controller;
Common::CubeCoordinate _coord;
protected:
void mousePressEvent(QGraphicsSceneMouseEvent* event);
};
#endif // HEXAGON_H
*.cpp
#include "hexagon.h"
Hexagon::Hexagon(QObject *parent):
QObject(parent)
{
/*another code*/
}
void Hexagon::mousePressEvent(QGraphicsSceneMouseEvent* event){
/*another code*/
}
On the other hand it still does not work since there is no interpolator for QBrush, so the solution is to implement an interpolator (use the interpolator of this solution)
static QVariant brushInterpolator(const QBrush &start, const QBrush &end, qreal progress)
{
QColor cstart = start.color();
QColor cend = end.color();
int sh = cstart.hsvHue();
int eh = cend.hsvHue();
int ss = cstart.hsvSaturation();
int es = cend.hsvSaturation();
int sv = cstart.value();
int ev = cend.value();
int hr = qAbs( sh - eh );
int sr = qAbs( ss - es );
int vr = qAbs( sv - ev );
int dirh = sh > eh ? -1 : 1;
int dirs = ss > es ? -1 : 1;
int dirv = sv > ev ? -1 : 1;
return QBrush(QColor::fromHsv( sh + dirh * progress * hr,
ss + dirs * progress * sr,
sv + dirv * progress * vr), progress > 0.5 ? Qt::SolidPattern : Qt::Dense6Pattern );
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
qRegisterAnimationInterpolator<QBrush>(brushInterpolator);
// ...
Another solution is to implement the same logic with QColor that does not need an interpolator:
*.h
#ifndef HEXAGON_H
#define HEXAGON_H
#include <QGraphicsPolygonItem>
#include <QObject>
class Hexagon : public QObject, public QGraphicsPolygonItem
{
Q_OBJECT
Q_PROPERTY(QColor color READ color WRITE setColor)
public:
explicit Hexagon(QObject *parent=nullptr);
QColor color() const;
void setColor(const QColor &color);
GameController* _controller;
Common::CubeCoordinate _coord;
protected:
void mousePressEvent(QGraphicsSceneMouseEvent* event);
};
#endif // HEXAGON_H
*.cpp
#include "hexagon.h"
#include <QBrush>
Hexagon::Hexagon(QObject *parent):
QObject(parent)
{
QBrush b = brush();
b.setStyle(Qt::SolidPattern);
setBrush(b);
/*another code*/
}
QColor Hexagon::color() const
{
return brush().color();
}
void Hexagon::setColor(const QColor &color)
{
QBrush b = brush();
b.setColor(color);
setBrush(b);
}
void Hexagon::mousePressEvent(QGraphicsSceneMouseEvent* event){
/*another code*/
}
Then you use "color" instead of "brush":
QPropertyAnimation* animation = new QPropertyAnimation(_Tilemap.at(tileCoord)->hexagon_, "color");
animation->setDuration(10000);
animation->setStartValue(QColor(Qt::yellow));
animation->setEndValue(QColor(Qt::blue));
animation->start();
Another simpler solution is to use QVariantAnimation:
auto it = _Tilemap.at(tileCoord)->hexagon_;
QVariantAnimation *animation = new QVariantAnimation;
QObject::connect(animation, &QVariantAnimation::valueChanged, [it](const QVariant & v){
it->setBrush(QBrush(v.value<QColor>()));
});
animation->setDuration(10000);
animation->setStartValue(QColor(Qt::yellow));
animation->setEndValue(QColor(Qt::blue));
animation->start();
My problem is that I have written this code inside Game::HandleInput() method but I cannot make the sf::Mouse::getPosition() method to get the mouse coordinates relative to window. Without argument, I don't get an error. However, ship doesn't rotate properly. I have tried getPosition(m_window) and getPosition(&m_window). I am getting this error:
no instance of overloaded function "sf::Mouse::getPosition" matches the argument list
EDIT: UPDATED THE WINDOW.H
Window.h:
class Window{
//Constructers
public:
Window();
Window(const std::string& l_title, const sf::Vector2u& l_size);
...
private:
sf::RenderWindow m_window;
...
}
EDIT: ADDED THE FULL CODE OF WINDOW.CPP:
Window.cpp:
#include "Window.h"
Window::Window() {
Setup("Window", sf::Vector2u(640, 480));
}
Window::Window(const std::string& l_title, const sf::Vector2u& l_size) {
Setup(l_title, l_size);
}
Window::~Window() {
Destroy();
}
void Window::Setup(const std::string& l_title,
const sf::Vector2u& l_size)
{
m_windowTitle = l_title;
m_windowSize = l_size;
m_isFullscreen = false;
m_isDone = false;
Create();
}
void Window::Create() {
auto style = (m_isFullscreen ? sf::Style::Fullscreen
: sf::Style::Default);
m_window.create({ m_windowSize.x, m_windowSize.y, 32 },
m_windowTitle, style);
}
void Window::Destroy() {
m_window.close();
}
void Window::Update() {
sf::Event event;
while (m_window.pollEvent(event)) {
if (event.type == sf::Event::Closed) {
m_isDone = true;
}
else if (event.type == sf::Event::KeyPressed &&
event.key.code == sf::Keyboard::F5)
{
ToggleFullscreen();
}
}
}
void Window::ToggleFullscreen() {
m_isFullscreen = !m_isFullscreen;
Destroy();
Create();
}
void Window::BeginDraw() { m_window.clear(sf::Color::Black); }
void Window::EndDraw() { m_window.display(); }
bool Window::IsDone() { return m_isDone; }
bool Window::IsFullscreen() { return m_isFullscreen; }
sf::Vector2u Window::GetWindowSize() { return m_windowSize; }
void Window::Draw(sf::Drawable& l_drawable){
m_window.draw(l_drawable);
}
EDIT: UPDATED THE GAME.H
Game.h:
class Game{
public:
Game();
~Game();
void HandleInput();
void Update();
void Render();
Window* GetWindow();
private:
...
Window m_window;
...
}
EDIT: UPDATED THE GAME.CPP
Game.cpp:
Game::Game() : m_window("Multiplayer Space Shooter Game", sf::Vector2u(800, 600)) {
// Setting up class members.
m_shipText.loadFromFile("C:\\Users\\AliTeo\\Desktop\\Piksel çalışmaları\\ship_pixel2.png");
m_ship.setTexture(m_shipText);
m_ship.setOrigin(m_shipText.getSize().x / 2, m_shipText.getSize().y / 2);
m_ship.setPosition(320, 240);
}
void Game::HandleInput() {
...
//Get the angle between ship and mouse.
//Error if there is an argument in getPosition()
m_angle = atan2(sf::Mouse::getPosition().y - m_ship.getPosition().y, sf::Mouse::getPosition().x - m_ship.getPosition().x); //TO DO: getPosition(&Relative To)
m_angle *= 180 / m_PI;
...
}
Window* Game::GetWindow() { return &m_window; }
EDIT: ADDED THE MAIN.CPP
Main.cpp
int main() {
Game game;
while (!game.GetWindow()->IsDone()) {
game.HandleInput();
game.Update();
game.Render();
}
}
First let me give you what I assume is a minimal example reproducing your problem:
#include <SFML/Graphics.hpp>
class MyWindow {
public:
MyWindow()
: m_window({800, 600, 32}, "my window title") {}
private:
sf::RenderWindow m_window;
};
int main() {
MyWindow window;
sf::Mouse::getPosition(window);
}
This code doesn't compile (and admittedly wouldn't do anything interesting when compiled, but that's not the point). I suspect it'd give you the same error that you're currently having if you tried to compile it.
Please note that this is what we expect when we talk about a MCVE: this code is short, simple, exhibits the error and would compile if not because of it.
Besides, it makes the error painfully clear, and if you tried to come up with a MCVE yourself, you may have solved your problem without having to post a question here, which would certainly save you time.
Contrast with your code:
m_angle = atan2(sf::Mouse::getPosition().y - m_ship.getPosition().y
,sf::Mouse::getPosition().x - m_ship.getPosition().x
);
//TO DO: getPosition(Relative To)
This code is legal, but you explained that it is incorrect and you wanted to turn it into something along those lines:
m_angle = atan2(sf::Mouse::getPosition(m_window).y - m_ship.getPosition().y
,sf::Mouse::getPosition(m_window).x - m_ship.getPosition().x
);
//TO DO: getPosition(Relative To)
... which doesn't compile.
However, in this scope m_window is a Window not a sf::RenderWindow!
The problem is that you're passing a reference to an object (MyWindow in my example, Window in your case) that encapsulates a sf::RenderWindow, but isn't convertible to sf::Window& itself.
Therefore, you can't pass it to sf::Mouse::getPosition which expects either nothing or a sf::Window&, but certainly not a Window& or a MyWindow&.
There are a lot of ways of fixing this. Two of which are presented below:
#include <SFML/Graphics.hpp>
class MyWindow {
public:
MyWindow()
: m_window({800, 600, 32}, "my window title") {}
// you could add an accessor
const sf::Window& getSfmlWindow() const { return m_window; }
// you may also expose a method to get the mouse position
// relatively to this window
const sf::Vector2i getMousePosition() const {
return sf::Mouse::getPosition(m_window);
}
private:
sf::RenderWindow m_window;
};
int main() {
MyWindow window;
sf::Vector2i mouse_position;
// this won't work! window isn't convertible to sf::Window&
// mouse_position = sf::Mouse::getPosition(window);
// using the accessor
mouse_position = sf::Mouse::getPosition(window.getSfmlWindow());
// or the exposed method
mouse_position = window.getMousePosition();
}
OS : Win10
Language : c++
Cocos2d-x Ver : 3.8.1
Tool : Visual Studio 2013
//MainMenuScene.h
#ifndef ProjectV_MainMenuScene_h
#define ProjectV_MainMenuScene_h
#include "cocos2d.h"
class MainMenuScene :public cocos2d::CCLayerColor
{
public:
virtual bool init();
static cocos2d::CCScene *createScene();
CREATE_FUNC(MainMenuScene);
cocos2d::Label *PlayLabel;
void goPlayScene(cocos2d::Ref *pSender);
};
#endif
//MainMenuScene.cpp
#include "MainMenuScene.h"
#include "PlayScene.h"
USING_NS_CC;
CCScene *MainMenuScene::createScene()
{
CCScene *scene = CCScene::create();
MainMenuScene *layer = MainMenuScene::create();
scene->addChild(layer);
return scene;
}
bool MainMenuScene::init()
{
if (!CCLayerColor::initWithColor(Color4B(255, 255, 255, 255)))
{
return false;
}
PlayLabel = Label::createWithTTF("Play", "fonts/consola.ttf", 18);
PlayLabel->setColor(Color3B::BLACK);
auto PlayBtn = MenuItemLabel::create(
PlayLabel,
CC_CALLBACK_1(MainMenuScene::goPlayScene, this));
// i thought CC_CALLBACK_1(MainMenuScene::goPlayScene,this) mean call goPlayScene(Ref* pSender) when click or touch the label
PlayBtn->setPosition(Vec2(240, 100));
auto pMenu = Menu::create(PlayBtn, NULL);
pMenu->setPosition(Vec2::ZERO);
this->addChild(pMenu);
return true;
}
void MainMenuScene::goPlayScene(Ref* pSender)
{
CCScene *pScene = PlayScene::createScene();
TransitionScene *pTransScene = TransitionFade::create(1.0f, pScene, Color3B::WHITE);
Director::getInstance()->replaceScene(pTransScene);
}
i don't know why function don't call when i clicked the label
You is not passed an argument in the calling function
CC_CALLBACK_1(MainMenuScene::goPlayScene, this)
You need either remove the Ref* pSender from void MainMenuScene::goPlayScene() or add an your sender in CC_CALLBACK_1(MainMenuScene::goPlayScene, this) after this.
can't change the color of a background i have this simple class :
here is the c++ file :
#include "HelloWorldScene.h"
USING_NS_CC;
HelloWorld::HelloWorld()
{
;
}
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;
}
// on "init" you need to initialize your instance
bool HelloWorld::init()
{
//////////////////////////////
// 1. super init first
if ( !LayerColor::initWithColor(Color4B(20,0,0,255)) )
{
return false;
}
winSize = Director::getInstance()->getWinSize();
visibleSize = Director::getInstance()->getVisibleSize();
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(Point(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(Point::ZERO);
this->addChild(menu, 1);
schedule( schedule_selector(HelloWorld::tick) );
return true;
}
void HelloWorld::onExit()
{
LayerColor::onExit();
}
void HelloWorld::onEnter()
{
LayerColor::onEnter();
auto cache = SpriteFrameCache::getInstance();
cache->addSpriteFramesWithFile("interface/sprites.plist", "interface/sprites.png");
SpriteBatchNode* batch = SpriteBatchNode::create("interface/sprites.png");
this->addChild(batch,BATCH_Z);
auto listener = EventListenerTouchAllAtOnce::create();
listener->onTouchesBegan = CC_CALLBACK_2(HelloWorld::onTouchesBegan, this);
listener->onTouchesMoved = CC_CALLBACK_2(HelloWorld::onTouchesMoved, this);
listener->onTouchesEnded = CC_CALLBACK_2(HelloWorld::onTouchesEnded, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
}
void HelloWorld::onTouchesBegan(const std::vector<Touch*>& touches, Event *event)
{
for( auto& touch : touches)
{
}
}
void HelloWorld::onTouchesMoved(const std::vector<Touch*>& touches, Event *event)
{
for( auto& touch : touches)
{
}
}
void HelloWorld::onTouchesEnded(const std::vector<Touch*>& touches, Event *event)
{
for( auto& touch : touches)
{
startAnim = true;
};
}
void HelloWorld::tick(float dt)
{
if(startAnim)
{
}
}
void HelloWorld::draw()
{
LayerColor::draw();
}
HelloWorld::~HelloWorld()
{
// Removes Touch Event Listener
_eventDispatcher->removeEventListener(_touchListener);
}
void HelloWorld::menuCloseCallback(Object* pSender)
{
Director::getInstance()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit(0);
#endif
}
and the h file :
#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__
#include "cocos2d.h"
class GameObj;
class ReelGameObj;
class HelloWorld : public cocos2d::LayerColor
{
public:
HelloWorld();
~HelloWorld();
// there's no 'id' in cpp, so we recommend returning the class instance pointer
static cocos2d::Scene* createScene();
// Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
virtual bool init();
// a selector callback
void menuCloseCallback(Object* pSender);
// implement the "static create()" method manually
CREATE_FUNC(HelloWorld);
void tick(float dt);
virtual void draw();
virtual void onEnter();
virtual void onExit();
void onTouchesBegan(const std::vector<cocos2d::Touch*>& touches, cocos2d::Event *event);
void onTouchesMoved(const std::vector<cocos2d::Touch*>& touches, cocos2d::Event *event);
void onTouchesEnded(const std::vector<cocos2d::Touch*>& touches, cocos2d::Event *event);
protected:
cocos2d::CustomCommand _customCommand;
void onDraw();
private:
cocos2d::EventListenerTouchOneByOne* _touchListener;
cocos2d::Size winSize;
cocos2d::Size visibleSize;
cocos2d::Point origin;
GameObj* pMainWindowObjCenter;
ReelGameObj* pReelGameObj;
bool startAnim;
};
#endif // __HELLOWORLD_SCENE_H__
and nothing no color in the background , why ?
im working on windows with VC 2012
I think your color is too dark. Try changing the values to (255,25,255,255) and check the result.
I created a sample project (in Beta 2), and only changed these lines:
.h:
class HelloWorld : public cocos2d::LayerColor
.cpp:
if( !LayerColor::initWithColor(Color4B(255,255,255,255)) )
The result is a white background.
I cleaned up and updated your code (I use cocos2dx 2.2.1) and I could change the layer's color to red.
#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__
#include "cocos2d.h"
class HelloWorld : public cocos2d::CCLayerColor
{
public:
HelloWorld();
~HelloWorld();
// there's no 'id' in cpp, so we recommend returning the class instance pointer
static cocos2d::CCScene* createScene();
// Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
virtual bool init();
// a selector callback
void menuCloseCallback(CCObject* pSender);
// implement the "static create()" method manually
CREATE_FUNC(HelloWorld);
virtual void onEnter();
void draw();
private:
cocos2d::CCSize winSize;
cocos2d::CCSize visibleSize;
cocos2d::CCPoint origin;
bool startAnim;
};
#endif // __HELLOWORLD_SCENE_H__
The cpp file
#include "HelloWorldScene.h"
USING_NS_CC;
HelloWorld::HelloWorld()
{}
HelloWorld::~HelloWorld()
{}
CCScene* HelloWorld::createScene()
{
// 'scene' is an autorelease object
auto scene = CCScene::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;
}
// on "init" you need to initialize your instance
bool HelloWorld::init()
{
//////////////////////////////
// 1. super init first
ccColor4B c = ccc4(255,0,0,255);
if ( !CCLayerColor::initWithColor(c) )
{
return false;
}
return true;
}
void HelloWorld::onEnter()
{
CCLayerColor::onEnter();
}
void HelloWorld::draw()
{
CCLayerColor::draw();
}