How to call vibrate effect in cocos2dx environment ?
Here is simple example code to call vibrate effect in cocos2d
This sample based on C++ with Visual studio
First, create service class to call vibrate whenever you want.
[DeviceServices.h]
class DeviceServices
{
public:
DeviceServices();
~DeviceServices();
// Android vibrate effect call method
void callVibrateEffect();
};
[DeviceServices.cpp]
#include "DeviceServices.h"
DeviceServices::DeviceServices(){}
DeviceServices::~DeviceServices(){}
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
#include "platform/android/jni/JniHelper.h"
#include <jni.h>
// important : provide your java class path
const char* AppActivityClassName = "com/eyen/cpp/AppActivity";
void DeviceServices::callVibrateEffect()
{
cocos2d::JniMethodInfo t;
if (cocos2d::JniHelper::getStaticMethodInfo(t, AppActivityClassName, "jniVibrateEffect", "()V"))
{
t.env->CallStaticVoidMethod(t.classID, t.methodID);
t.env->DeleteLocalRef(t.classID);
}
}
#else
const char* AppActivityClassName = "com/eyen/cpp/AppActivity";
Important : red marked is that, your vibrate implemented method in java path.
for me like this, but you might be different.
If you finished create service class,
Next step is easy, just call like below example code.
#include "DeviceServices.h"
void MainScene::hitEnermyObj()
{
// call android vibrate effect
DeviceServices::vibrateEffect();
}
All is done in C++ (cocos2dx part)
Next step is that, implement vibrate method for Android.
My java file path is "com/eyen/cpp/AppActivity" that which has contained implemented code for android vibrate
package com.eyen.cpp;
import android.os.Vibrator;
import org.cocos2dx.lib.Cocos2dxActivity;
public class AppActivity extends Cocos2dxActivity {
// main
private static AppActivity _appActiviy;
// vibrate
Vibrator mVibrator = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
_appActiviy = this;
mVibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
}
public void vibrateEffect() {
mVibrator.vibrate(80); // red marked number is that power of vibrate (bigger number is powerfull)
}
public static void jniVibrateEffect() {
_appActiviy.runOnUiThread(new Runnable() {
@Override
public void run() {
_appActiviy.vibrateEffect();
}
});
}
}
Good!
All is done, enjoy developement.
Here is a game which is used vibrated effect for android.
Download "Fast Shooter" in google playstore
Link : https://play.google.com/store/apps/details?id=com.eyen.fastshooter
'개발 > 코딩' 카테고리의 다른 글
Trouble shoot for does not have android.permission.SEND_SMS case in Android (0) | 2016.09.29 |
---|---|
잘되던 안드로이드 문자보내기 기능이 권한문제때문에 갑자기 안될때(does not have android.permission.SEND_SMS) (0) | 2016.09.29 |
cocos2d 리스트뷰 클릭 이벤트가 호출되지 않을때 문제해결방법(Cocos2dx listview event / addEventListenerListView not call) (0) | 2016.09.28 |
Trouble shoot for cocos2dx listview click event is not working case (0) | 2016.09.28 |
Cocos2dx 개발시 android 기기 진동 호출 방법 (코코스2d 개발 진동구현) (0) | 2016.09.21 |
내 이미지로 bmfont 직접 만들기 (image manager 사용법) (0) | 2016.08.31 |
cocos2d 에서 bmfont 오류 발생할 경우 문제해결방법 (import error) (0) | 2016.08.31 |
간편한 bmfont 설치 및 사용법 (앱 개발에 추천) (0) | 2016.08.31 |
최근댓글