(I am new to Cocos2dx)
(Xcode with macOS, Android studio with Galaxy Note5)
macOS = Version 10.15.6
Xcode = Version 11.6
Android Studio = Version 4.0.1
Phone (Galaxy Note5) = samsung sm-n920k
I am making a stopwatch similar to https://cstimer.net.
The stopwatch is a cocos2d::Label* stopWatchLabel;
I am using the void update(float dt);
function to update the stopwatch(Label) using the setString();
function.
It seems to work on Xcode. (Image below) (It is running)
But it doesn’t seem to work on Android Studio. As soon as I start the stopwatch, the program crashes. I think it’s because of the setString();
function. But I am not sure, and I can’t fix it.
How can I fix this code to make the stopwatch on Android?
HelloWorldscene.cpp
#include "HelloWorldScene.h"
#include "ui/CocosGUI.h"
#include <iostream>
USING_NS_CC;
Scene* HelloWorld::createScene()
{
auto scene = HelloWorld::create();
auto layer = Layer::create();
scene->addChild(layer);
return scene;
}
// 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 HelloWorld::init()
{
//////////////////////////////
// 1. super init first
if ( !Scene::init() )
{
return false;
}
auto visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();
/////////////////////////////
// 3. add your codes below...
// colors for stopWatchLabel
WHITE_4B = Color4B(255, 255, 255, 255);
PINK_4B = Color4B(255, 0, 126, 255);
GREEN_4B = Color4B(0, 255, 126, 255);
stopWatchLabel = Label::createWithTTF("0.0->", "fonts/FiraCode-Regular.ttf", 96);
stopWatchLabel->setPosition(Vec2(visibleSize.width/2+origin.x, visibleSize.height/2-100+origin.y));
this->addChild(stopWatchLabel, 1);
stopWatchBase = 0;
this->scheduleUpdate();
stopWatchCheck = 0; // 0: stopped, 1: started, 2: anti ovelap int usage, 3: stop with color
touchDurationCheck = 0; // 0, 1->
touchDurationBase = 0.0;
holdStopWatch = 0.55;
listener1 = EventListenerTouchOneByOne::create();
listener1->onTouchBegan = [=](Touch* touch, Event* event){
if (stopWatchCheck == 0)
{
stopWatchBase = 0;
stopWatchLabel->setTextColor(PINK_4B);
touchDurationCheck = 1;
}
else if (stopWatchCheck == 1)
{
stopWatchLabel->setTextColor(PINK_4B);
stopWatchCheck = 0;
}
return true;
};
listener1->onTouchMoved = [=](Touch* touch, Event* event){
};
listener1->onTouchEnded = [=](Touch* touch, Event* event){
if (stopWatchCheck == 2)
{
stopWatchCheck = 1;
}
stopWatchLabel->setTextColor(WHITE_4B);
touchDurationCheck = 0;
touchDurationBase = 0;
};
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener1, this);
return true;
}
void HelloWorld::update(float dt)
{
// stopWatchCheck to these
if (stopWatchCheck == 1)
{
stopWatchBase += dt;
stopWatchLabel->setString(std::to_string(stopWatchBase));
}
// touchDurationCheck
if (touchDurationCheck == 1)
{
touchDurationBase += dt;
}
if (touchDurationBase > holdStopWatch && listener1->onTouchBegan)
{
stopWatchLabel->setString(std::to_string(stopWatchBase)); // if thing
stopWatchLabel->setTextColor(GREEN_4B); // if thing
stopWatchCheck = 2; // if thing
}
}
HelloWorldScene.h
#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__
#include "cocos2d.h"
class HelloWorld : public cocos2d::Scene
{
public:
static cocos2d::Scene* createScene();
virtual bool init();
// a selector callback
void menuCloseCallback(cocos2d::Ref* pSender);
// implement the "static create()" method manually
CREATE_FUNC(HelloWorld);
void update(float dt); // update function
float stopWatchBase; // stopwatch base number thing
cocos2d::Label* stopWatchLabel; // label to represent
int stopWatchCheck;
cocos2d::EventListenerTouchOneByOne* listener1; // touch listener
int touchDurationCheck;
float touchDurationBase; // touch duration
float holdStopWatch; // hold to start
cocos2d::Color4B WHITE_4B;
cocos2d::Color4B PINK_4B;
cocos2d::Color4B GREEN_4B;
};
#endif // __HELLOWORLD_SCENE_H__
2 posts - 2 participants
Read full topic