how to rotate a node in cocosjs? - cocos2d-iphone

I am new to Cocos Creator.
trying to rotate a node with action.
openGates(){
const gateLeft = this.node.getChildByName("gateLeft")
const gateRight = this.node.getChildByName("gateLeft")
const rotateLeft = cc.RotateBy.create(2, 40);
const rotateRight = cc.RotateBy.create(2, -40);
gateLeft.runAction(rotateLeft)
gateRight.runAction(rotateRight)
},
and I got this error
Uncaught TypeError: cc.RotateBy.create is not a function
any idea?

please use const rotateLeft = new cc.RotateBy(2, 40);
instead of using create method.
also there is ";" missing in your first two statements

Related

Unable to pass the string variable to Sprite in cocos2dx

I'm having problem with the strings in cocos2dx & C++. I want to pass the variable background to Sprite::create(background) however, I get an error. If it was in java the following code will work, but since I'm not used to C++ it may be different. Plus, if it was a int how will I pass it ? How will I be able to solve this? Some tips or samples will be great! I will love to hear from you!
void GameLayer::initBackground()
{
UserDefault *_userDef = UserDefault::getInstance();
//int型
auto _int =_userDef->getIntegerForKey("back");
auto string background = "Background1.png";
if (_int == 0) {
background = "Background2.png";
}
auto bgForCharacter = Sprite::create(background);
bgForCharacter->setAnchorPoint(Point(0, 1));
bgForCharacter->setPosition(Point(0, WINSIZE.height));
addChild(bgForCharacter, ZOrder::BgForCharacter);
auto bgForPuzzle = Sprite::create("Background2.png");
bgForPuzzle->setAnchorPoint(Point::ZERO);
bgForPuzzle->setPosition(Point::ZERO);
addChild(bgForPuzzle, ZOrder::BgForPuzzle);
}
auto userDefault=UserDefault::getInstance();
int value=userDefault->getIntegerForKey("back"); //find value for back if exist then it return that value else return 0
auto sprite = Sprite::create(value==0?"Background2.png":"Background1.png");
sprite->setPosition(100, 100);
this->addChild(sprite, 0);
When you want to change your background just put any value rather than 0
UserDefault::getInstance()->setIntegerForKey("back", 1);

Don't understand these C++ box2d Errors

I've trying to get this C++ method to work in my iOS game.
-(b2Vec2) RayCheckWithInput:p1 andX:p2
{
b2RayCastInput input;
input.p1 = p1;
input.p2 = p2;
input.maxFraction = 1;
//check every fixture of every body to find closest
float closestFraction = 1; //start with end of line as p2
b2Vec2 intersectionNormal(0,0);
for (b2Body* b = self.world.world->GetBodyList(); b; b = b->GetNext()) {
for (b2Fixture* f = b->GetFixtureList(); f; f = f->GetNext()) {
b2RayCastOutput output;
if ( ! f->RayCast( &output, input ) )
continue;
if ( output.fraction < closestFraction ) {
closestFraction = output.fraction;
intersectionNormal = output.normal;
}
}
}
b2Vec2 intersectionPoint = p1 + closestFraction * (p2 - p1);
return intersectionPoint;
}
I know a fair bit about obj-c, but not C++. Here's a screen shot of the errors for that method it's showing.
Any help appreciated (in Chrome you can do right click and open image in new tab to see it better)
You need to be more explicit with the variables passed into the Objective-C function. You are passing without a type so it is not able to infer that it is actually a C++ type rather than an Objective-C type.
Try something like:
-(b2Vec2) RayCheckWithInput: (CPPTye*)p1 andX: (CPPTye*)p2
Edit: a quick check of the box2d docs says you need something like this:
-(b2Vec2) RayCheckWithInput: (b2Vec2)p1 andX: (b2Vec2)p2
Edit 2: Also worth noting that the Raycast function takes a third parameter childIndex as described in the docs.

About terminate called after throwing an instance of 'std::length_error'

I am just now trying to show sprite on stage using CCSprite.
The character class is a class which has succeeded to the CCSprite.
I wrote the code as following:
bool HellowWorld::init() method:
bool HelloWorld::init()
{
int Indexes[9] = {5,11,15,16,17,19,20,21,25};
int x;
int y;
CCArray *filename = new CCArray();
filename->addObject(new CCString("1-1.png"));
filename->addObject(new CCString("2-1.png"));
filename->addObject(new CCString("1-2.png"));
filename->addObject(new CCString("2-3.png"));
filename->addObject(new CCString("2-3.png"));
filename->addObject(new CCString("1-2.png"));
filename->addObject(new CCString("2-3.png"));
filename->addObject(new CCString("1-1.png"));
filename->addObject(new CCString("1-1.png"));
for(int i=0;i<9;i++){
const char *MyImage = ((CCString*)filename->objectAtIndex(i))->getCString();
characters *chara = (characters*)CCSprite::create(MyImage);
x = Indexes[i]%HORIZONTAL_AXIS;
y = Indexes[i]/HORIZONTAL_AXIS;
CCPoint point = ccp(MARGIN_WIDTH + PIPE_WIDTH * x,MARGIN_HEIGHT + PIPE_HEIGHT * y);
chara->setPosition(point);
this->addChild(chara,1);
}
But, I am getting the following error message:
libc++abi.dylib: terminating with uncaught exception of type std::length_error: basic_string::_S_create
How should I solve this issue?
There are definitely some coding issues here.
const char *MyImage = ((CCString*)filename->objectAtIndex(i))->getCString();
characters *chara = (characters*)CCSprite::create(MyImage);
You are doing a lot of casting and it is confusing. Your cast to (characters*) is just wrong.
characters seems like it needs to subclass CCSprite*, I hope that it does.
then you can do something like:
characters *chara = characters::create(filename->objectAtIndex(i)->getValue());
If your for loop you have a hardcoded 9. You might change that to filename->count() that way it always loops to the size of the CCArray.
Here is the docs for CCSprite: http://www.cocos2d-x.org/reference/native-cpp/V2.2.3/d8/de9/group__sprite__nodes.html#ga0e84a5bd213dc1556503a4120243a390

EXC_BAD_ACCESS issue after refactoring

The following code works in a the Marmalade simulator (I'm on OSX using x-code)
bool PictureDictionary::OnTableSelect(CTable* table, int tab){
//if something is selected, look up the item, and display it
//also change the search to the selected item
if(-1 < tab){
// if a term is selected, set the search text field to the term
CString term = m_SearchResults.GetString(tab);
if(m_currentWord != (char*)term.Get()){
m_currentWord = (char *)term.Get();
m_searchTextField->SetAttribute("text", term);
char* normalizedTerm = (char *)term.Get();
char* imagePath;
sprintf(imagePath,"images/%s.jpg", normalizedTerm);
if(m_ImageAttached){
m_Image->SetAttribute("image", (const char*)imagePath);
} else {
m_Image = CreateImage(CAttributes()
.Set("name", "picture")
.Set("x1", "0")
.Set("x2", "0")
.Set("y1", "50%")
.Set("image", (const char*)imagePath)
);
m_SearchView->AddChild(m_Image);
m_ImageAttached = true;
}
}
}
return true;
}
When I run the simulator, and select an item from the table, the image appears, and changes when I select a different item. When I go to refactor, I get a EXC_BAD_ACCESS (code=1…..) Error
bool PictureDictionary::OnTableSelect(CTable* table, int tab){
//if something is selected, look up the item, and display it
//also change the search to the selected item
if(-1 < tab){
// if a term is selected, set the search text field to the term
CString term = m_SearchResults.GetString(tab);
if(m_currentWord != (char*)term.Get()){
m_currentWord = (char *)term.Get();
m_searchTextField->SetAttribute("text", term);
char* normalizedTerm = (char *)term.Get();
char* imagePath;
sprintf(imagePath,"images/%s.jpg", normalizedTerm);
UpdatePictureView(imagePath);
}
}
return true;
}
void PictureDictionary::UpdatePictureView(char* imagePath){
if(m_ImageAttached){
m_Image->SetAttribute("image", (const char*)imagePath);
} else {
m_Image = CreateImage(CAttributes()
.Set("name", "picture")
.Set("x1", "0")
.Set("x2", "0")
.Set("y1", "50%")
.Set("image", (const char*)imagePath)
);
m_SearchView->AddChild(m_Image);
m_ImageAttached = true;
}
}
Any suggestions on how to clean up the code without getting these issues?
Edit RE Comments about uninitialized variables:
m_ImageAttached was initialized to false in the constructor, unless I'm doing something wrong. Also, changing the condition to check if m_Image!=NULL also throws the same error.
main.cpp:
PictureDictionary pictDict(myApp, &dictionary);
Constructor for PictureDictionary:
PictureDictionary::PictureDictionary(CAppPtr app,Dictionary::Dictionary* dictionary){
m_App = app;
m_Dictionary = dictionary;
m_currentWord = "";
m_ImageAttached = false;
}
imagePath is an unitialized pointer, in both snippets. Any attempt to dereference is undefined behaviour. It just appeared to work in the first snippet. Use an array or populate a std::string instead:
std::string imagePath(std::string("images/") + normalizedTerm + ".jpg");
And use std::string::c_str() if access to the underlying const char* is required.

CCFiniteTimeAction::getDuration() EXC_BAD_ACCESS when creating a CCSequence

I was able to run this code without getting an error on the iPhone simulator. However, when I run it on my iPhone, I am getting a EXC_BAD_ACCESS error. Here is the code:
CCAnimate * explosionAnimate = CCAnimate::create(explosionAnimation);
CCCallFuncN * callFuncN = CCCallFuncN::create(this,callfuncN_selector(GameLayer::removeChildFromParent));
CCFiniteTimeAction * explosionSequence = CCSequence::create(explosionAnimate, callFuncN);
CCSprite * explosionSprite = CCSprite::createWithSpriteFrameName("explosion_frame_1");
addChild(explosionSprite);
explosionSprite->setPosition(point);
explosionSprite->runAction(explosionSequence);
}
void GameLayer::removeChildFromParent(CCNode * child)
{
child->removeFromParent();
}
The error occurs when CCSequence::create(...) is called. Debugging through CCSequence::create(...)
CCFiniteTimeAction* CCSequence::create(CCFiniteTimeAction *pAction1, va_list args)
{
CCFiniteTimeAction *pNow;
CCFiniteTimeAction *pPrev = pAction1;
while (pAction1)
{
pNow = va_arg(args, CCFiniteTimeAction*);
if (pNow)
{
pPrev = createWithTwoActions(pPrev, pNow);
}
else
{
break;
}
}
return pPrev;
}
I am seeing that "createWithTwoActions" get's called twice. That doesn't seem right. On the 2nd call to "createWithTwoActions". The error occurs within CCFiniteTimeAction, specifically at the getDuration() inline function:
inline float getDuration(void) { return m_fDuration; }
Any ideas why this would be occurring?
I was able to resolve the issue by calling CCSequence::create() like:
CCSequence::create(explosionAnimate, callFuncN, NULL);
instead of:
CCSequence::create(explosionAnimate, callFuncN);
I'm guessing this has to do with the nature of CCFiniteAction pointers and C++ variable length arguments.