Manual Animation doesn't work in Cocos2d-x - animation

I'm testing manual animation. However, the animation seems to have some issues, so it doesn't work.
These are the code. Can you take a look and let me where I'm wrong.
HelloWorldScene.h
#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__
#include "cocos2d.h"
#include <string>
USING_NS_CC;
class HelloWorld : public cocos2d::CCLayerColor{
public:
// Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
virtual bool init();
// there's no 'id' in cpp, so we recommend returning the class instance pointer
static cocos2d::CCScene* scene();
// a selector callback
void menuCloseCallback(CCObject* pSender);
// implement the "static node()" method manually
CREATE_FUNC(HelloWorld);
virtual void onEnter();
protected:
CCSprite* m_grossini;
};
#endif // __HELLOWORLD_SCENE_H__
HelloWorldScene.cpp
#include "HelloWorldScene.h"
USING_NS_CC;
CCScene* HelloWorld::scene()
{
CCScene *scene = CCScene::create();
HelloWorld *layer = HelloWorld::create();
scene->addChild(layer);
return scene;
}
bool HelloWorld::init()
{
if ( !CCLayerColor::initWithColor( ccc4(255,255,255,255) ) )
{
return false;
}
CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();
CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
"CloseNormal.png",
"CloseSelected.png",
this,
menu_selector(HelloWorld::menuCloseCallback));
pCloseItem->setPosition(ccp(origin.x + visibleSize.width - pCloseItem->getContentSize().width/2 ,
origin.y + pCloseItem->getContentSize().height/2));
CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
pMenu->setPosition(CCPointZero);
this->addChild(pMenu, 1);
CCSize winSize = CCDirector::sharedDirector()->getWinSize();
m_grossini = CCSprite::create("grossini.png");
m_grossini->retain();
this->addChild(m_grossini);
m_grossini->setPosition(ccp(winSize.width/2, winSize.height/2));
return true;
}
void HelloWorld::menuCloseCallback(CCObject* pSender)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
CCMessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
#else
CCDirector::sharedDirector()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit(0);
#endif
#endif
}
void HelloWorld::onEnter(){
CCAnimation* animation = CCAnimation::create();
for( int i=1;i<15;i++)
{
char szName[100] = {0};
sprintf(szName, "grossini_dance_%02d.png", i);
animation->addSpriteFrameWithFileName(szName);
}
// should last 2.8 seconds. And there are 14 frames.
animation->setDelayPerUnit(2.8f / 14.0f);
animation->setRestoreOriginalFrame(true);
CCAnimate* action = CCAnimate::create(animation);
m_grossini->runAction(CCSequence::create(action, action->reverse(), NULL));
}
I hope you can help me. Thank you so much!

Try following things:..
First of all remove this line m_grossini->retain(); because addchild automatically increase its retain count.
Found the problem..
As you are overriding the OnEnter method you need to call it manually for the base class. In your case adding the line:-
CCLayerColor::onEnter();
in the OnEnter method will do the work.
In future be careful while overriding base class methods.

try this code:
CCAnimation* animation = CCAnimation::create();
for( int i=1;i<15;i++)
animation->addSpriteFrameWithFileName((CCString::createWithFormat("grossini_dance_%02d.png",i)->getCString()));
// should last 2.8 seconds. And there are 14 frames.
animation->setDelayPerUnit(2.8f / 14.0f);
animation->setRestoreOriginalFrame(true);
CCAnimate* action = CCAnimate::create(animation);
m_grossini->runAction(action); //Run once all frame(OR)
m_grossini->runAction(CCRepeatForever::create(action)); //Run RepeatForever

Related

How can i use VALA delegates in GTK3 button callback?

I'm trying to understand Vala delegates with Gtk3.
I tested callback and lambda with no problem.
I wanna test a delegate callback, here my code :
using Gtk;
delegate void typeDelegate(Button button);
int main (string[] args) {
Gtk.init (ref args);
typeDelegate cb = cbLabelf;
var window = new Window ();
window.title = "First GTK+ Program";
window.border_width = 10;
window.window_position = WindowPosition.CENTER;
window.set_default_size (350, 70);
window.destroy.connect (Gtk.main_quit);
var button = new Button.with_label ("Click me!");
//button.clicked.connect (cb);
//button.clicked+= cb;
button.clicked.connect+=cb;
window.add (button);
window.show_all ();
Gtk.main ();
return 0;
}
void cbLabelf(Button button)
{
button.label = "tank yu";
}
I also red generated C code ( when i use lambda) to understand.
Here the compil error :
GTKsampleDelegate.vala:20.5-20.30: error: Arithmetic operation not supported for types Gtk.Button.clicked.connect' andtypeDelegate'
button.clicked.connect+=cb;
Well,
Seems that you want to get the intrinsic variable that holds the instance that emitted the signal, I find strange that vala doesn't let you use a delegate variable to obtain it via parameter, yet, you can use one of the forms below: using no delegation variable (A) or bypassing the error with a closure (B).
public class FooSignalClass : Object {
/* Gtk Button.clicked signal has the void f(void) signature */
public signal void on_foo ();
public void foo() {
on_foo();
}
}
public delegate void FooSignalFunc (FooSignalClass fooer);
void on_foo_handler (FooSignalClass fooer) {
long fooer_memory_address = (long)fooer;
GLib.message(#"fooer exists? $(fooer!=null).");
GLib.message(#"address=$fooer_memory_address.");
}
int main () {
var foo_signal = new FooSignalClass();
long fooer_memory_address = (long)foo_signal;
GLib.message(#"foo_signal address=$fooer_memory_address.");
/* Option A: Connect directly without the delegate variable */
foo_signal.on_foo.connect(on_foo_handler);
/* Option B: You cant use a delegate directly, bypass it with a closure */
FooSignalFunc func = on_foo_handler;
foo_signal.on_foo.connect((instance) => {
func(instance);
});
foo_signal.foo();
return 0;
}

Problems with PBL_IF_ROUND_ELSE function

I'm trying to follow the tutorial about Watchfaces creation, but I'm stuck.
I copied the code from the wiki so there shouldn't be any error whatsoever, but I'm getting this error while compiling
error: implicit declaration of function 'PBL_IF_ROUND_ELSE' [-Werror=implicit-function-declaration]
I tried to google it but I couldn't find anything useful.
This is the code
#include <pebble.h>
static Window *s_main_window;
static TextLayer *s_time_layer;
static void update_time() {
// Get a tm structure
time_t temp = time(NULL);
struct tm *tick_time = localtime(&temp);
// Write the current hours and minutes into a buffer
static char s_buffer[8];
strftime(s_buffer, sizeof(s_buffer), clock_is_24h_style() ? "%H:%M" : "%I:%M", tick_time);
// Display this time on the TextLayer
text_layer_set_text(s_time_layer, s_buffer);
}
static void tick_handler(struct tm *tick_time, TimeUnits units_changed) {
update_time();
}
static void main_window_load(Window *window) {
// Get information about the Window
Layer *window_layer = window_get_root_layer(window);
GRect bounds = layer_get_bounds(window_layer);
// Create the TextLayer with specific bounds
s_time_layer = text_layer_create(
GRect(0, PBL_IF_ROUND_ELSE(58, 52), bounds.size.w, 50));
// Improve the layout to be more like a watchface
text_layer_set_background_color(s_time_layer, GColorClear);
text_layer_set_text_color(s_time_layer, GColorBlack);
text_layer_set_text(s_time_layer, "00:00");
text_layer_set_font(s_time_layer, fonts_get_system_font(FONT_KEY_BITHAM_42_BOLD));
text_layer_set_text_alignment(s_time_layer, GTextAlignmentCenter);
// Add it as a child layer to the Window's root layer
layer_add_child(window_layer, text_layer_get_layer(s_time_layer));
}
static void main_window_unload(Window *window) {
// Destroy TextLayer
text_layer_destroy(s_time_layer);
}
static void init() {
// Create main Window element and assign to pointer
s_main_window = window_create();
// Set handlers to manage the elements inside the Window
window_set_window_handlers(s_main_window, (WindowHandlers) {
.load = main_window_load,
.unload = main_window_unload
});
// Show the Window on the watch, with animated=true
window_stack_push(s_main_window, true);
// Make sure the time is displayed from the start
update_time();
// Register with TickTimerService
tick_timer_service_subscribe(MINUTE_UNIT, tick_handler);
}
static void deinit() {
// Destroy Window
window_destroy(s_main_window);
}
int main(void) {
init();
app_event_loop();
deinit();
}
I'm using the CloudPebble SDK.
I got it, PBL_IF_ROUND_ELSE works only with 3.X SDK (I was using the 2.X).
"PBL_IF_ROUND_ELSE(58, 52)" Only works with Pebble Time or SDK3.
You can replace "PBL_IF_ROUND_ELSE(58, 52)" with a range between 0 and 100. 0 meaning it will be at the top of the screen. 100 means it will be at the bottom of the screen.
Replace the "PBL_IF_ROUND_ELSE(58, 52)" with a 0. That should do the trick. Im assuming you are trying to run this program on the aplite version?

cocos2d-x v3 sprite animation issue

I have a little cocos2d-x v3 Windows project and I want an animated sprite. The sprite I have in an object named Worker with other parameters. To run the animation I tried the following, but it always gives a segmentation fault:
//worker.cpp:
...
void Worker::initAnimationWalk() {
spritebatch = cocos2d::SpriteBatchNode::create("robo.png");
auto cache = cocos2d::SpriteFrameCache::getInstance();
cache->addSpriteFramesWithFile("robo.plist");
worker = cocos2d::Sprite::createWithSpriteFrameName("robot1.png");
//spritebatch->addChild(worker);
Vector<SpriteFrame *> animFrames(3);
char str[100] = {0};
for(int i = 1; i <= 3; i++)
{
sprintf(str, "robot%i.png", i);
cocos2d::SpriteFrame* frame = cache->getSpriteFrameByName( str );
animFrames.pushBack(frame);
}
animation = cocos2d::Animation::createWithSpriteFrames(animFrames, 0.2f);
}
cocos2d::Animation *Worker::getAnimationWalk() {
return animation;
}
...
//Worker.h
#ifndef __WORKER_H__
#define __WORKER_H__
#include "cocos2d.h"
class Worker
{
public:
...
void initAnimationWalk();
cocos2d::Animation *getAnimationWalk();
private:
cocos2d::Animation *animation;
cocos2d::Sprite *worker;
cocos2d::SpriteBatchNode *spritebatch;
};
#endif
//Now i want the animation in my scene:
//HellowWorldScene.cpp
void HelloWorld::setMyAction() { //all Worker Objects in an vector
vector<Worker>::iterator it = workerVector.begin();
for(int i = 0; i<workerVector.size();i++) {
it->getWorker()->stopAllActions();
auto actionAnim = Animate::create(it->getAnimationWalk());
if(it->getWorker()->getPosition().x > 0.50*2*p.x) {
auto action = cocos2d::MoveTo::create(it->getSpeed(),Point(0,it->getWorker()->getPosition().y));
auto sp = Spawn::createWithTwoActions(action, actionAnim);
it->getWorker()->runAction(sp);
it->getWorker()->setFlipX(true);
} else {
auto action = cocos2d::MoveTo::create(it->getSpeed(),Point(2*p.x,it->getWorker()->getPosition().y));
auto sp = Spawn::createWithTwoActions(action, actionAnim);
it->getWorker()->runAction(sp);
it->getWorker()->setFlipX(false);
}
}
it++
}
Any help?
SpriteAnimation auto change images using Animation.
Animation *animation2=Animation::create();
animation2->setDelayPerUnit(0.7f);//Time Duration change Images
animation2->addSpriteFrameWithFile("img_1.png");
animation2->addSpriteFrameWithFile("img_2.png");
animation2->addSpriteFrameWithFile("img_3.png");
AnimationCache::getInstance()->addAnimation(animation2, "animation2");
SpriteName->runAction(Sequence::create(Animate::create(animation2),NULL));//this animation Only One time
OR
SpriteName->runAction(RepeatForever::create(Sequence::create(Animate::create(animation2),NULL)));//If Repeate this animation write it..
SpriteAnimation auto change images using Animation.
Animation *anim=Animation::create();
anim->setDelayPerUnit(0.7f);//Time Duration change Images
anim->addSpriteFrameWithFile("img_01.png");
anim->addSpriteFrameWithFile("img_02.png");
anim->addSpriteFrameWithFile("img_03.png");
AnimationCache::getInstance()->addAnimation(anim, "anim");
SpriteNM->runAction(Sequence::create(Animate::create(anim),NULL));//this animation Only One time
OR
SpriteNM->runAction(RepeatForever::create(Sequence::create(Animate::create(anim),NULL)));//If Repeate this animation write it..

Unity3D - GUIText Does Not Show On Collision With Object

Ive been following this article online and have swapped a few names and tags about but I dont seem to see any text appearing on the screen...
Here is my PlayerCollision script:
#pragma strict
function Update () {
}
function OnControllerColliderHit(hit : ControllerColliderHit){
if(hit.gameObject.tag == "Collider"){
ShowMessage.message = "HELLO WORLD";
ShowMessage.turnTextOn = true;
}
}
This is my ShowMessage script:
#pragma strict
static var turnTextOn : boolean = false;
static var message : String;
private var timer : float = 0.0;
function Start(){
timer = 0.0;
turnTextOn = false;
guiText.text = "";
}
function Update () {
if(turnTextOn){
guiText.enabled = true;
guiText.text = message;
timer += Time.deltaTime;
}
if(timer >= 5){
turnTextOn = false;
guiText.enabled = false;
timer = 0.0;
}
}
I have linked the ShowMessage script to my GUIText object and have linked the PlayerCollision script with the CharacterController. There is also a box collider object with the Collision tag I also Have the GUIText in view just to rule that out.
Anyone any idea what is wrong? Thanks
The tag should be "Collider" not "Collision" because if(hit.gameObject.tag == "Collider")
Problem solved. Turns out I ticked isTrigger which stopped the collision from being detected. This gives the problem of the character not being able to move through the object. What i did was tick the isTrigger option and change OnControllerColliderHit(hit : ControllerColliderHit) to OnTriggerEnter (obj : Collider)

Safari plugin crashes on NPN_GetValue

My plugin code crashes when I call the NPN_GetValue. Basically I created a scriptable object which has a 'getDevice' method that can return a device array to JavaScript. Below is the code snippet.
static bool mainNPObjectInvoke(NPObject *obj, NPIdentifier identifier, const NPVariant *args, uint32_t argCount, NPVariant *result)
{
printf("create main object");
MainNPObject *mainObject = (MainNPObject *)obj;
if (identifier == methodIdentifiers[METHOD_ID_GET_DEVICES])
{
NPObject *windowObj = NULL;
browser->getvalue(mainObject->npp, NPNVWindowNPObject, &windowObj);
// it crashed here
....
}
}
I created the MainNPObject instance with below method.
NPObject *createMainNPObject(NPP npp)
{
MainNPObject *object = (MainNPObject *)browser->createobject(npp, &mainNPClass);
object->npp = npp;
theMainObject = object;
return (NPObject *)object;
}
The createMainNPObject is called in the plugin function I provided to browser.
NPError NPP_GetValue(NPP instance, NPPVariable variable, void *value)
{
PluginObject *obj = instance->pdata;
switch (variable) {
case NPPVpluginCoreAnimationLayer:
if (!obj->rootLayer)
setupLayerHierarchy(obj);
*(CALayer **)value = obj->rootLayer;
return NPERR_NO_ERROR;
case NPPVpluginScriptableNPObject:
if (!obj->mainObject)
{
obj->mainObject = createMainNPObject(instance);
}
....
}
And the allocate function is as below.
static NPObject *mainNPObjectAllocate(NPP npp, NPClass *class)
{
initializeIdentifiers();
MainNPObject *mainObject = malloc(sizeof(MainNPObject));
mainObject->deviceManager = [[DeviceManager alloc] init];
return (NPObject *)mainObject;
}
Definition of MainNPObject:
typedef struct
{
NPObject *npobject;
NPP npp;
DeviceManager *deviceManager;
} MainNPObject;
By debugging the code, I found that the system raised an EXC_BAD_ACCESS when calling the browser->getValue and it looks like the npp pointer is invalid.
0x00007fff83f82dab <+0019> je 0x7fff83f82db9 <_ZN6WebKit14NetscapePlugin7fromNPPEP4_NPP+33>
0x00007fff83f82dad <+0021> incl 0x8(%rax)
Can someone help me out?
Thanks!
Hmm; not seeing anything obvious. Try adding another parameter (an int?) to your structure and set it during allocate or immediately afterwords, then later on check to see if it's still the value you set before you call getvalue. See if your struct is somehow getting corrupt. That happened to me once when I was casting the NPObject funny in a non-obvious way.

Resources