Skip to content

Instantly share code, notes, and snippets.

@sdkfz181tiger
Created June 19, 2014 00:57
Show Gist options
  • Save sdkfz181tiger/a942a9025ad18a16333f to your computer and use it in GitHub Desktop.
Save sdkfz181tiger/a942a9025ad18a16333f to your computer and use it in GitHub Desktop.
Cocos2dx_ver3.0(時計)
//
// ClockSprite.cpp
// Oyadius3
//
// Created by Shimeji Ozaki on 2014-06-17.
//
//
#include "ClockSprite.h"
ClockSprite::ClockSprite(float posX, float posY,
float vX, float vY):CharSprite(posX, posY, vX, vY){
this->milTime = 0;
}
ClockSprite::~ClockSprite(){
}
ClockSprite* ClockSprite::create(float posX, float posY,
float vX, float vY){
ClockSprite* sprite = new ClockSprite(posX, posY, vX, vY);
if(sprite && sprite->initWithSpriteFrameName("space_25.png")){
sprite->autorelease();
sprite->setPosition(Point(posX, posY));
sprite->makeStage();
return sprite;
}
CC_SAFE_DELETE(sprite);
return NULL;
}
void ClockSprite::update(float dt){
if(getMoveEnabled()){
setPosX(posX + vX * dt);
setPosY(posY + vY * dt);
this->setPosition(Point(posX, posY));
}
}
// born
void ClockSprite::born(){
this->setAliveEnabled(true);
}
// die
void ClockSprite::die(){
this->setAliveEnabled(false);
}
// intersect
bool ClockSprite::intersect(const Rect& targetRect){
return this->boundingBox().intersectsRect(targetRect);
}
// makeStage
void ClockSprite::makeStage(){
time_t tNow = time(NULL);
struct tm* lNow = localtime(&tNow);
int hour = lNow->tm_hour;
int min = lNow->tm_min;
int sec = lNow->tm_sec;
float centerY = this->boundingBox().size.width * 0.5f;
char cn[10];
sprintf(cn, "%02d:%02d:%02d", hour, min, sec);
myLabel = CCLabelBMFont::create(cn, "MisakiGothic.fnt");
myLabel->setColor(Color3B(255, 255, 255));
myLabel->setPosition(Point(centerY, this->boundingBox().size.height/2));
this->addChild(myLabel);
}
// updateTime
void ClockSprite::updateClock(){
time_t tNow = time(NULL);
struct tm* lNow = localtime(&tNow);
int hour = lNow->tm_hour;
int min = lNow->tm_min;
int sec = lNow->tm_sec;
char cn[10];
sprintf(cn, "%02d:%02d:%02d", hour, min, sec);
myLabel->setString(cn);
}
//
// ClockSprite.h
// Oyadius3
//
// Created by Shimeji Ozaki on 2014-06-17.
//
//
#ifndef __Oyadius3__ClockSprite__
#define __Oyadius3__ClockSprite__
#include "CharSprite.h"
#include "UtilMath.h"
#include <time.h>
USING_NS_CC;
class ClockSprite:public CharSprite{
private:
LabelBMFont* myLabel;
int milTime;
public:
ClockSprite(){};
ClockSprite(float posX, float posY, float vX, float vY);
~ClockSprite();
static ClockSprite* create(float posX, float posY,
float vX, float vY);
// update
virtual void update(float dt);
// born
virtual void born();
// die
virtual void die();
// intersect
virtual bool intersect(const Rect& targetRect);
// init
void makeStage();
// updateTime
void updateClock();
};
#endif /* defined(__Oyadius3__ClockSprite__) */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment