Okay, so I want my cat sprite to move up and down onClick of two (UP and DOWN) buttons.
I'm a beginner in cocos2d-x.
So, in mygame.h i have a global declaration of the sprite cat:
cocos2d::Sprite *cat;
In one function i create a new scene and add a cat in it.
cat = Sprite::create("cat.png");
cat->setScale(0.2);
cat->setPosition(0, 190);//(Director::getInstance()->getVisibleOrigin().x + 50, Director::getInstance()->getVisibleSize().height / 2);
layer->addChild(cat);
playscene->addChild(cat);
In another function(button callback) i have this code:
void HelloWorld::down(Object* pSender){
CCActionInterval* down = CCMoveBy::create(1.0f, Point(0.0, -20.0));
cat->runAction(down);
}
And everything's ok untill i press the up or down button.
It throws an error on the cat->runAction(down); line.
When i exemine the variable cat, it looks like I cant get to the position parameters. Its a memory read error..
In cocos2dx 3.0
you can write direct in runaction for any sprite.
spriteName->runAction(Sequence::create(MoveBy::create(1.0f,Point(398,565)),NULL));
It looks like you are mixing Cocos2D-X 2.x API's with Cocos2D-X 3.0 ones. I'm taking a stab in the dark guess and saying it looks like you're trying to use 3.0. You will need to change the following line:
CCActionInterval* down = CCMoveBy::create(1.0f, Point(0.0, -20.0));
To:
ActionInterval* down = MoveBy::create(1.0f, Point(0.0, -20.0));
Related
Currently I'm trying to make an mobile app and I need to turn on the flash light of a mobile device. Actually I want to use it as a torch(On/off/blink etc). I searched in the qt docs and found QCamera class. But I'm unable to turn the light on. I'm using Qt 6.3.1. Can anyone help?
I'm doing something like this:
// In constructor of a widget class,
cam = new QCamera(QCameraDevice::BackFace, this); //cam is declared in the header file
// In a function, called after a button click,
// cam->setFlashMode(QCamera::FlashOn);
cam->setTorchMode(QCamera::TorchOn);
cam->start();
I added this code inside a function and called it after a button-click event. But when I click the button, nothing happens.
UPD:
What I have found interesting is, I've tried printing the return value of isFlashModeSupported() and it returns false!
QString str = cam->isFlashModeSupported(QCamera::FlashOn) ? "Flash: Yes" : "Flash: No";
ui->Flash->setText(str); // str = "Flash: No"
I'm using a phone which has controllable flash light. So what can be the reason for this kind of behaviour?
I've got a project that is a mix of pure C++ and Objective-C++ in order to incorporate some C++ libraries.
I've tried adding some basic SCNScenes into the mix. (By basic I mean a scene with a box node in it and that's it). Every time i get the error:
Assertion failed: (renderSize.x != 0), function -[SCNRenderContextMetal _setupDescriptor:forPass:isFinalTechnique:], file /BuildRoot/Library/Caches/com.apple.xbs/Sources/SceneKit/SceneKit-332.6/sources/Core3DRuntime/NewRenderer/SCNRenderContextMetal.mm, line 688.
Does anyone know what causes this, and if so how can I get round it?
EDIT:
In my ViewController.mm I've got:
self.sceneView = [[SCNView alloc] initWithFrame:frame];
self.sceneView.scene = [SCNScene scene];
SCNNode *cube = [SCNNode nodeWithGeometry:[SCNBox boxWithWidth:1.0 height:1.0 depth:1.0 chamferRadius:0]];
cube.geometry.firstMaterial.diffuse.contents = [UIColor redColor];
[self.sceneView.scene.rootNode addChildNode:cube];
[self.view addSubview:self.sceneView];
Sounds like you are starting up your SceneKit scene using a storyboard.
If so, the recent version of the SDK now requires that you set the constraints on views or else they end up having trivial size. It might just be a function of setting constraints on your SceneKit scene.
You also need to set the frame to something valid if it isn't. eg
CGRect frame = [[UIScreen mainScreen] applicationFrame];
I discovered that SceneKit throws a fit if you set the SCNView frame to CGRectZero. There has to be at least 1 pixel of rendering real estate. Simple as that.
I am programming with Cocos2d 3.0 now, In Cocos2d 2.0, we can use the following code to add accelerometer to app, but this example was based on class CCLayer which has deprecate in Cocos2d 3.0, and UIAccelerometer also replaced by CMMotionManager in IOS 5.0, so I am wondering how to do this in Cocos2d 3.0? I googled for a while, didn't find anything useful.
-(id) init
{
if ((self = [super init]))
{
// ...
self.isAccelerometerEnabled = YES;
// ...
}
}
-(void) accelerometer:(UIAccelerometer *)accelerometer
didAccelerate:(UIAcceleration *)acceleration
{
// ...
}
===
We've written a tutorial on exactly this: https://www.makegameswith.us/gamernews/371/accelerometer-with-cocos2d-30-and-ios-7
You need to use the CoreMotion framework.
Well, there are two problems in the tutorial example given above.
Single instance of CMMotionManager.
Acceleration data become +Ve or -Ve according to the orientation of the device. You also need to add Scene as observer of device orientation change notification.
If you don't want handle these overheads, you can use CCAccelerometer class. It solves both the problems.
HOW TO USE
Add CoreMotion Framework in your project from Build Phases.
Copy CCAccelerometer.h and CCAccelerometer.m files in your project.
Import CCAccelerometer.h file in the Prefix.pch.
Implement the <CCSharedAccelerometerDelegate> in the CCScene where you want to use the accelerometer.
Create shared instance in init method by simply calling [CCAcceleroMeter sharedAccelerometer];
Start accelerometer in -(void)onEnterTransitionDidFinish by calling [CCAcceleroMeter sharedAccelerometer]startUpdateForScene:self];
Define delegate method -(void)acceleroMeterDidAccelerate:(CMAccelerometerData*)accelerometerData in your scene.
Stop accelerometer in -(void)onExitTransitionDidStart by calling [CCAcceleroMeter sharedAccelerometer]stopUpdateForScene:self];
You can find out the example project in GitHub.
Here is example:
Device::setAccelerometerEnabled(true);
auto accelerometerListener = EventListenerAcceleration::create([this](Acceleration* acc, Event* event)
{
});
getEventDispatcher()->addEventListenerWithSceneGraphPriority(accelerometerListener, this);
Also video tutorial https://www.youtube.com/watch?v=Xk6lXK6trxU
I am translating a piece of code from cococs2D to cocos2D-X. I came across the following lines that i cannot fathom out how to translate
[spriteBg runAction:[CCSequence actions:sc,[CCCallFuncO actionWithTarget:basketTimer_ selector:NSSelectorFromString([selectors objectAtIndex:0]) object:sprite], nil]];
Can someone please help me translate this to Cocos2d in Cocos2d-X ?
Kind Regards,
try this..
spriteBg->runAction::create(CCSequence::create(sc,CCCallFunc::create(this, callfunc_selector(myMethod)),NULL));
In your code the myMethod is replaced by the selector at the index 0 of the "selectors" which I'm guessing is an array of the selectors or a dictionary or something like that.
and "sc" is a predefined action which is to be run on spriteBg.
For cocos2d-x v 2.2.1:
NSSelectorFromString([selectors objectAtIndex:0] <- in cocos2d-x you can't create selector from string, so you must know function you want call, or keep selectors in container(but I never do this)
CCCalFuncO *call = CCCallFuncO::create(basketTimer_, callfuncO_selector(BasketTimerClass::BasketTimerMethod), sprite)
spriteBg->runAction(CCSequence::create(sc, call, NULL));
Every class in cocos2d-x and cocos2d-iphone have the same name, so you can easly find it in documentation:
CCSequence CCCalFuncO
Topic In Cocos2d-android game:
To delete the sprite after collided with another sprite, i have used spriteRect function, but this isnt making the sprite to get removed after intersect, after lot of googling got to know that it should be deleted from parent,
here's the code
CGRect ship1Rect = CGRect.make(ship1.getPosition().x - (ship1.getContentSize().width/2),
ship1.getPosition().y - (ship1.getContentSize().height/2),
ship1.getContentSize().width,
ship1.getContentSize().height);
if (CGRect.intersects(targetRect, ship1Rect))
{
parent.removeChildByTag(17, true);
}
but here parent.removeChildByTag(17, true); in this line getting error as "parent cannot be resolved" error, where am i going wrong please can anybody tell
ship1.getParent().removeChild(ship1,true);
or
ship1.getParent(). removeChildByTag(17,true);
You can use only
removeChild(ship1,true);
insteasd of
parent.removeChildByTag(17, true);