In my game I have an animated sprite. I added box2d to my game to add gravity. now I want to update my sprite's position using the schedule function. I got it working with objective-C, but I want it to be a multiplatform game so now I'm trying it in c++.
In my init I have the following line:
this->schedule(cocos2d::SEL_SCHEDULE(View::tick(1.0f)));
and the method tick:
void View::tick(float dt) {
world->Step(dt, 10, 10);
for(b2Body *b = world->GetBodyList(); b; b=b->GetNext()) {
if(b->GetUserData() != NULL) {
}
}
}
I get the following error on the line in my init: "cannot cast from type ' void' to member pointer type 'coco2d::sel_schedule' (aka void coco2d::CCobject::*)(float)
I have no idea what to do now. I tried to google, but everyone either uses objective - c or schedule_selector (which apparently doesn't work anymore)
Thanks in advance
You should probably not be using the schedule function to update a single sprite.
In your scene class, override the virtual method for update(...):
virtual void update(float dt)
{
CCLOG("This was called.");
// Do updates for everything in the scene you need to call on a tick.
}
Also, override onEnter() and onExitTransitionDidStart(). In the first, call scheduleUpdate to set up your scene for automatically being called on a tick. In the second, call unscheduledUpdate (as the scene is exiting) to stop the updates.
void IntroScene::onEnter()
{
CCScene::onEnter();
scheduleUpdate();
}
void IntroScene::onExitTransitionDidStart()
{
CCScene::onExitTransitionDidStart();
unscheduleUpdate();
}
Doing it this way gives you explicit control over what gets updated when...you only have a single function called during an update cycle.
HOWEVER, if you really want to do it:
Declare your custom updater like this:
void IntroScene::myUpdate(float dt)
{
CCLOG("Also called");
}
Schedule it like this:
schedule(schedule_selector(IntroScene::myUpdate), 1.0f);
Unschedule it like this:
unschedule(schedule_selector(IntroScene::myUpdate));
** NOTE ** As far as I know, you can only "schedule" objects derived from CCNode.
Was this helpful?
Related
The Situation
My company has a QML-based application which displays some content using a custom OpenGL-based render plugin (MyGame). This plugin has a few critical needs:
To be able to effect changes in the renderer in response to QML-based signals.
(e.g. change the position of an object rendered by the game)
To only process these changes at a specific spot in MyGame's redraw loop.
(This is very important; MyGame is very sensitive about when changes are allowed.)
To have the plugin redraw at 60Hz (at least).
The Problem
The code we have right now honors (1) and (2), but fails (3); the plugin does not get visually updated consistently. (The updates are erratic, at an estimated 5-10Hz.) I believe that the plugin we have created—based on QQuickFramebufferObject—is not taking proper advantage of how Qt/QML intended the scene graph to be updated.
How can I re-structure my plugin so that I get all three of the above?
The Code
Overview:
The plugin creates a QQuickFramebufferObject (MyPlugin) and a QQuickFramebufferObject::Renderer (MyRenderer).
When MyRenderer::render() is called it calls MyGame::Redraw() itself, and then calls update().
MyGame::Redraw() does what it needs to, and at the right spot where changes can be accepted, emits a timeToMakeChanges QML signal on MyPlugin.
QML listens for the onTimeToMakeChanges signal and invokes methods on the plugin that affect MyGame.
To workaround the problem of low-frequency visual updates, I've found that if I overlay a QML Canvas over my plugin and redraw the canvas frequently using a Timer, my plugin starts to get visually updated at what appears to be around 60Hz. Clearly this is a gross hack.
Following is a summary of the code setup. Please forgive missing/incorrect code; I'm trying to distill thousands of lines of glue code down to the essentials for this question.
MyPlugin.h
#include <QOpenGLFramebufferObject>
#include <QQuickFramebufferObject>
class MyPlugin : public QQuickFramebufferObject {
Q_OBJECT
public:
MyPlugin();
virtual ~MyPlugin();
virtual QQuickFramebufferObject::Renderer* createRenderer() const;
signals:
void timeToMakeChanges();
public slots:
void makeChanges(QVariant inValue);
void HandleWindowChanged(QQuickWindow *inWindow);
private:
MyGame* GetGame() { ... }
};
MyPlugin.cpp
#include "MyPlugin.h"
#include <MyGame.h>
// ******************************************************************
class MyRenderer:
public QObject,
public QQuickFramebufferObject::Renderer,
protected QOpenGLFunctions
{
Q_OBJECT
public:
virtual void render();
private:
static void RequestGameChanges();
};
void MyRenderer::render() {
if ( !m_Initialized ) {
QOpenGLFramebufferObject *theFbo = this->framebufferObject();
InitializeGl( theFbo ); // Not shown
m_MyGame = &MyGame::Create();
m_MyGame->RegisterCallback(
reinterpret_cast<qml_Function>(MyRenderer::RequestGameChanges)
);
m_Initialized = true;
}
m_MyGame->RestoreState();
m_MyGame->Redraw();
m_MyGame->SaveState();
m_PluginItem->window()->resetOpenGLState();
// Tell QML that we want to render again as soon as possible
update();
}
// This gets invoked in the middle of m_MyGame->Redraw()
void MyRenderer::RequestGameChanges() {
emit m_PluginItem->timeToMakeChanges();
}
// ******************************************************************
MyPlugin::MyPlugin() {
setMirrorVertically(true);
connect(
this, SIGNAL(windowChanged(QQuickWindow*)),
this, SLOT(HandleWindowChanged(QQuickWindow*))
);
}
void MyPlugin::HandleWindowChanged(QQuickWindow *inWindow) {
inWindow->setClearBeforeRendering(false);
}
void MyPlugin::makeChanges(QVariant inValue) {
MyGame *theGame = GetGame();
// Send the requested changes to theGame
}
QQuickFramebufferObject::Renderer* MyPlugin::createRenderer() const {
m_Renderer = new MyRenderer( *this );
}
MyApp.qml
import MyPlugin 1.0
Window {
MyPlugin {
property var queuedUpChanges: ([])
onSomeOtherSignal: queueUpChangesToMake();
onTimeToMakeChanges: makeChanges( queuedUpChanges );
}
Canvas { id:hack }
Timer {
interval:10; running:true; repeat:true
onTriggered: hack.changeWhatYouShow();
}
}
Bonus Points
The main question is "How do I modify my code so that I get 60Hz updates?" However, as seen in the QML, the setup above requires me to queue up all changes in QML so that they are able to be applied during the right spot in the MyGame::Render().
Ideally, I'd prefer to write QML without timeToMakeChanges, like:
MyPlugin {
onSomeOtherSignal: makeChanges( ... );
}
If there's a way to accomplish this (other than queuing up the changes in C++ instead)—perhaps something related to synchronize() I'd love to know about it.
I'd make a timer in QML that calls the makeChanges regularly. But store all the state in MyPlugin. Then, in Renderer::synchronize(), copy from MyPlugin to MyRenderer, so it can be used by the MyGame.
(although, I wouldn't do any gamelogic-related calculations in QML ever in the first place)
I am struggling to make my code more object orientated.
I have a small program that wishes to accomplish 2 very simple states: An input state, and a result state.
The input state seems simple to resolve as although it is graphical it is "self-updating." The user drops sprites and removes sprites on to the screen to produce an input.
The result state is annoying me because I have produced some very ugly code for it, that is not at all Object Orientated.
This state is required to do things sequentially and I am struggling to find examples of how that is done with Objects. It's basically some animation with the same object: here is some Pseudo-Code.
Static int objectx = 120, object y = 120;
Static int state=0
switch(state)
{
Case 0:
//....move object right
//....once object is far enough right
state = 1;
Case 1:
//....move object down
//....once object is far enough down
state = 2;
...etc
So I am guessing it needs to move to having some sort of state engine, but I am struggling to see how to accomplish sequential events using states. These states are always the same, and so can be hard coded, they will not change based upon the input given.
Any help will be most gratefully recieved.
UPDATE
Maybe we could think about this second state as a cut-scene in a 2d game. We want a character to walk on to the screen, say something and then walk off.
So the way I'm doing it at the moment is managing this part of the programs state via the switch statement. This function is called every time we are in the "result" part of our program, and we are using the switch statement to update the positions of our sprites. Once we have reached the end of the first set of movements we move to the next switch statement and keep doing that until it is completed. It works but I was hoping to use a " gamestate " Class, that could take ownership of the graphics and sound, and move things as appropriate.
Note: making some assumptions here because I don't have any context.
It sounds like each sprite should have its own cycle, rather than the entire game logic moving about the sprites.
Adapting this into an object-orientated design, you can wrap each sprite in some
class:
class NPC : Sprite {
private:
Position CurrentPosition;
int CurrentState;
public:
virtual void Think() = 0;
virtual void Render() = 0;
};
Then you can inherit from this class for specific sprites:
class GobbledyGook : NPC {
private:
const int FinalState = 10;
public:
bool Completed = false;
void Think() override {
if(Completed)
return;
switch(CurrentState) {
// ... repeating logic here ...
}
CurrentState++;
if(CurrentState == FinalState)
Completed = true;
}
void Render() override {
// ... draw the sprite ...
}
}
From the main game logic you can then update every sprite:
// Spawn a GobbledyGook into existence.
my_npcs.insert(new GobbledyGook());
// In frame logic.
bool finished = true;
for( NPC* n : my_npcs )
{
n->Think();
if(!n->Completed)
finished = false;
}
if(finished) {
// Result state.
}
// In render logic.
for( NPC* n : my_npcs )
{
n->Render();
}
You can naturally adopt this logic for entire scenes too:
class Intro : Cutscene {
private:
vector<NPC*> SceneSprites;
public:
void Think() override {
switch(CurrentState) {
...
}
for( NPC* n : SceneSprites ) {
n->Think();
}
}
...
};
As for whether you should be removing or changing the use of states, what do you intend to gain from doing that?
It's hard to recommend a different approach without knowing all the flaws of the current approach.
And again, trying to implement bezier curves redactor. There is
class BezierNode : public QGraphicsItem
BezierNode::BezierNode(QPointF point, Type type) : QGraphicsItem()
{
setPos(point);
setFlags(ItemIsMovable | ItemSendsScenePositionChanges | ItemSendsGeometryChanges);
}
It properly moves around in scene on mousePress + mouseMove, and I can catch events in itemChange() for some additional acting with control points. In QGraphicsItem::mouseMoveEvent() (according to Qt source on gitorious) there is a call to item->setPos(...). However, if I try to reimplement BezierNode::setPos(..), it's never triggered on moving object.
void BezierNode::setPos(const QPointF &pos) {
qDebug() << "setPos " << pos;
m_point = pos;
QGraphicsItem::setPos(pos);
}
In my case setPos() triggers only in constructor (there I call it manually). Yes, it moves in scene properly, I can get its position with pos() and use it everywhere instead of m_point, but I want to understand, what happens there.
Thanks in advance.
QGraphicsItem::setPos() is not virtual, so you can't override it. That's why BezierNode::setPos() will never be called.
I've started working with cocos2d few days ago...and im working on small 2d game.
I figured out how to animate sprites. Now when sprite finished with animation I want it to be cleared from screen.
How to pass argument to callback function?
target.runAction(CCSequence.actions(repeatAnimation,
CCCallFuncND.action(this, "deleteTarget",target)));
Function is defined as:
public void deleteTarget(Object target)
It always gives me "NoSuchMethodException"...any idea?
Do it like this
CCCallFuncN ccfun = CCCallFuncN.action(this, "test");
public void test(Object sender) {
Do whatever you require
}
I'm working on a arduino project.
For those who don't know what a arduino is:
arduino is a little electronic-board whit a micro-controller which can be programmed in C++
Since a arduino is a little micro-controller, it is single core 16MHz multiprocessing is very hard.
I have made a class, in th constructor, it defines a pin as pwm output en sets min fade, max fade, fade time etc.
I have methods to start fading and stop fading change setpoint, speed...
for multifading I've made a update method.
So when I call light1.update() it checks if the pwm output needs to be changed or not.
Now the question is:
I have more lights so that will be:
light1.update();
light2.update();
light3.update();
and so on
Is there a way to write just one line to call update() on every object of the fade class?
Looks like you're looking for a combination of the Observer pattern combined with an Instance manager pattern.
Whenever you create a new instance of Light, add it to the manager. When you want the events to be triggered, iterate through the instances in the manager and call update.
Something like:
class LightManager
{
static std::vector<Light> lights;
static void notify()
{
for ( size_t i = 0 ; i < lights.size() ; i++ )
lights[i].update();
}
static void add(const light& l)
{
lights.push_back(l);
}
};
class Light
{
Light()
{
LightManager::add(*this);
}
};
Then you update all lights with:
LightManager::notify();