Keep the phone screen ON in Cocos2dx - android

I created a game for Android using Cocos2DX 3.4. I'm using the acelerometer for the player to move around the screen so I don't need to touch the screen. The problem is that the screen turn off when I play for a while. I need to know how to keep the phone awake even if I don't touch the screen.

Just Write
cocos2d::Device::setKeepScreenOn(true);
on the First Scene you load.

I found one solution, but I´m still waiting for a better one if any:
Just added this to my CPP:
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
#include "../cocos2d/cocos/platform/android/jni/Java_org_cocos2dx_lib_Cocos2dxHelper.h"
#endif
Then I added this init method to my main Scene:
bool HelloWorld::init()
{
if ( !LayerColor::initWithColor( Color4B(204,204,204,255)))
{
return false;
}
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
setKeepScreenOnJni(true);
#endif
return true;
}

Related

how to open the camera using OpenCV for Android VideoCapture

I am working wiht OpenCV4Android, and I am trying to use VideoCapture class to open the Android Camera and perform further processings on each captured frame.
Hi i'm working on android with opencv and i'm sorry to tell you that you can not open a stream with opencv in cpp. The ndk android dont give any API to access the camera so opencv can not open any stream. I saw one time an API for android 4.4 if i remember well but i did not succeed to open any thing.
Since the realase of android 7.0 you have access to some C function that give you the right to take a picture, check out this header: camera/NdkCameraManager.h.
And if you whant a beginning of code
#include <camera/NdkCameraManager.h>
#include <android/log.h>
#define LOGI(...) ((void)__android_log_print(ANDROID_LOG_INFO, "gandoulf", __VA_ARGS__))
#define LOGW(...) ((void)__android_log_print(ANDROID_LOG_WARN, "gandoulf", __VA_ARGS__))
void AndroidCamera()
{
ACameraIdList *cameraList; //list of available camera
ACameraManager *cameraManager; // android camera manager
camera_status_t cameraStatus; // enum for the error while using camera
cameraManager = ACameraManager_create(); // instantiate the camera manager
cameraStatus = ACameraManager_getCameraIdList(cameraManager, &cameraList); // get the list of available camera, return enum camera_status_t for the error
if (cameraStatus == ACAMERA_OK) {
LOGI("cameraList ok\n");
LOGI("num of camera = %d", cameraList->numCameras);
}
else
LOGW("ERROR with cameraList\n");
}
With that you have the list of camera and you can normaly take a picture with function that you can find in the header.

Unity3d Gyroscope look around

I'm new to Unity and I am trying to build a solar system exploration app through unity. I have the environment set up, and now all I need is the ability to look around (via tilting and moving the phone itself, which is android) smoothly. I have the ability to look around, but if I do a complete 180, it seems to invert the physical orientation of the phone with the visual movements in game, e.g. if I have turn 180 degrees, if I tilt the phone down it shifts my vision in game to the right, up results in visual shift to the left. Here is the code I have thus far:
#pragma strict
private var quatMult : Quaternion;
private var quatMap : Quaternion;
function Start () {
Input.gyro.enabled = true;
}
function Update () {
#if UNITY_ANDROID
quatMap = Input.gyro.attitude;
#endif
transform.localRotation = Quaternion.Euler(90, 0, 0) * quatMap * Quaternion(0,0,1,0) /*quatMult*/;
}
Any help is greatly appreciated. Thanks.
This should be what you're looking for: https://gist.github.com/chanibal/baf46307c4fee3c699d5. Just drag it to the camera and it should work.
You might want to remove the reset on touch part (Input.touchCount > 0 in Update) and debug information (the OnGui method).

Draw line on mouse move and detect line collision in Unity2D and Unity3D

I used the code for line collison from http://www.theappguruz.com/tutorial/draw-line-mouse-move-detect-line-collision-unity2d-unity3d/ and it's working all right. It detects when line collides with itself, but I need to check collision with other objects (lets say a platform). I added this function in script:
private bool isLineCollidedWithOtherObject()
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Camera.main.WorldToScreenPoint(pointsList[pointsList.Count-1]));
if(Physics.Raycast(ray,out hit))
{
if(hit.collider)
return true;
}
return false;
}
and also added while checking for collision
if(isLineCollide() && isLineCollidedWithOtherObject());
But it's not working. I added Box2dcollider to platform (Player). What am I doing wrong?
take a look at this thread, specifically the linked post from the devs about raycasting from the camera in 3d space in a 2d sprite game:
http://forum.unity3d.com/threads/unity-2d-raycast-from-mouse-to-screen.211708/#post-1424457
the docs for the functions mentioned are here: http://docs.unity3d.com/ScriptReference/Physics2D.GetRayIntersection.html

Multitouch is not working on Marmalade + Cocos2d-X

I am using Marmalade to create a project using Cocos2d-X, however I am not being able to use Multitouch in my project. The idea would be to identify pinch gestures to zoom in and out.
I've enabled touches in my GameLayer.cpp file using:
this->setTouchMode(kCCTouchesAllAtOnce);
this->setTouchEnabled(true);
I've also added the configuration below to my application's ICF file:
[S3E]
AndroidPointMultiEnable = 1
I have tested my application both on the simulator (enabling multitouch there) and in an Android tablet, but in the simulator the touches are not enven registed by my application and on the Android tablet I am receiving each touch as a separate event and not both at the same time.
Could you help me?
Thanks
UPDATE
Here is my code for ccTouchesBegan:
void GameLayer::ccTouchesBegan(CCSet* pTouches, CCEvent* event)
{
CCSetIterator it;
CCTouch *touch;
CCPoint touchA;
CCPoint touchB;
IwTrace(APPLICATION, ("Touches Began - touch count: %d", pTouches->count()));
it = pTouches->begin();
if (usePinchGesture && pTouches->count() == 2)
{
touch = (CCTouch*) (*it);
touchA = touch->getLocation();
it++;
touch = (CCTouch*) (*it);
touchB = touch->getLocation();
pinchDistance = GeomHelper::getDistanceSq(touchA, touchB);
IwTrace(APPLICATION, ("Pinch gesture detected. Starting distance between points: %f", pinchDistance));
}
}
The problem is that the count of touches pTouches->count() is always 1, so each touch event gets treated separately.
Thanks
Yep, pTouches->count() is always 1 in android!
cocos2d-x v2.2.3
in ..\cocos2dx\platform\android\jni\TouchesJni.cpp
blablabla...
JNIEXPORT void JNICALL Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeTouchesBegin(JNIEnv * env, jobject thiz, jint id, jfloat x, jfloat y) {
cocos2d::CCDirector::sharedDirector()->getOpenGLView()->handleTouchesBegin(1, &id, &x, &y);
}
It's always 1 in the first param.
On android, the multi-touch is open by default. You don’t need to open anything before get the touch coordinates in void MyLayer::ccTouchesBegan/Moved/Ended

AndEngine - lagging when playing sound

I'm creating a simple game with AndEngine. A ball is dropped toward a floor, and whenever it collides with the floor, I want to play a short colliding sound. In method onUpdate(), I check for collision and play sound accordingly.
I use class Sound for playing sound (as in the SoundExample of AndEngine). Testing on Samsung Galaxy S2.
The problem is the program gets lagged when the sound is played. And it even affects game physics (sometimes the ball bounces higher than the highest point when disabling sound).
This is the code:
public void onUpdate(float pSecondsElapsed) {
// mSound.play();
if (this.mSprite.collidesWith(ball.getSprite())) {
if (!colliding && mSound != null){ // play sound for first collision only
mSound.play();
colliding = true;
}
}
else{
colliding = false;
}
}
If I remove mSound.play() or keep playing sound (remove comment at line 2), the program works smoothly.
Does anyone encounter the same problem? And have a solution to get rid of the lag? Many thanks!
as that you mentioned that it works smoothly when you keep playing the sound .. then the problem is not with the sound
the collidesWith() method is probably your culprit, remember that onUpdate gets called every frame .. maybe you'll have to redesign your code or limit the number of frames per second [change your engine options to use a FixedStepEngine to achieve that]

Categories

Resources