how to use the accelerometer in libgdx what is the specific code - libgdx

I have a problem using the accelerometer in libgdx because I am new at it and I want to know how to make the accelerometer. So can you please send me the code so I can use it in my game. P.S I am a sixth grader and be very clear when you answer the question.

float accelX = Gdx.input.getAccelerometerX();
float accelY = Gdx.input.getAccelerometerY();
if (accelX < -1){
//go up
}
if (accelY < -1 ){
//go left
}
if (accelX > +1 ){
/go down
}
if (accelY > +1){
//go right
}
use that code to move something ok?

To use it is very easy!
Gdx.input.getAccelerometerX();
Gdx.input.getAccelerometerY();
Gdx.input.getAccelerometerZ();
You get a value from -10 to 10. 0 Would mean that the device is not tilting at all.
To test is write in your render / update method this to debug your accelerometer movement
System.out.println( Gdx.input.getAccelerometerX()+" "
Gdx.input.getAccelerometerY()+" "+
Gdx.input.getAccelerometerZ());
Now start your game and look at the logcat while you are tilting your phone!

Related

If hitTestObject = true return current position on screen?

I'm making a 2d platform game and i'm not knowledgeable enough to know how to set up collisions with my character and platforms.
I basically want when fireboy1 hits basePlatform, fireboy1 to not be able to go any further
I have got this code so far i'm just struggling to understand what to put in the if statement itself.
I have commented the part I am unsure about.
I'm not sure if this is the right way to go about it.
public function platformCollision():void
{
if (fireboy1.hitTestObject(basePlatform))
{
fireboy1.y = fireboy1.y = //current position on screen?
}
}
If you don't want fireboy1 to go past the basePlatform, you should probably do something like:
fireboy1.y = basePlatform.y - fireboy1.height
Please note that this all depends on both fireboy1 and basePlatform having top-left orientation.
no need for a hitTestObject....
lets assume that the regstration point for fireboy and the base platform are in its centre:
public function loop(e:Event){
if(fireboy1.y + fireboy1.height > basePlatform.y - basePlatform.height){
fireboy1.y = basePlatform.y - fireboy1.height;
}
}

how to pinch zoom picture from point of gesture as3

I create an image object and I was able to enlarge .
but images can only be in view of the center of the image .
so how I can enlarge the image of the point of the gesture ??
picture.addEventListener(TransformGestureEvent.GESTURE_ZOOM, onZoom);
function onZoom(e:TransformGestureEvent){
picture.scaleX *= (e.scaleX+e.scaleY)/2;
picture.scaleY *= (e.scaleX+e.scaleY)/2;
}
Try this...
import flash.events.TransformGestureEvent;
import flash.ui.Multitouch;
import flash.ui.MultitouchInputMode;
import fl.motion.MatrixTransformer;
Multitouch.inputMode = MultitouchInputMode.GESTURE;
stage.addEventListener(TransformGestureEvent.GESTURE_ZOOM , onZoom);
function onZoom(event:TransformGestureEvent):void
{
//trace(e.localX);
var locX:Number=event.localX;
var locY:Number=event.localY;
var stX:Number=event.stageX;
var stY:Number=event.stageY;
var prevScaleX:Number=gambar.scaleX;
var prevScaleY:Number=gambar.scaleY;
var mat:Matrix;
var externalPoint=new Point(stX,stY);
var internalPoint=new Point(locX,locY);
gambar.scaleX *= event.scaleX;
gambar.scaleY *= event.scaleY;
if(event.scaleX > 1 && gambar.scaleX > 6){
gambar.scaleX=prevScaleX;
gambar.scaleY=prevScaleY;
}
if(event.scaleY > 1 && gambar.scaleY > 6){
gambar.scaleX=prevScaleX;
gambar.scaleY=prevScaleY;
}
if(event.scaleX < 1.1 && gambar.scaleX < 1){
gambar.scaleX=prevScaleX;
gambar.scaleY=prevScaleY;
}
if(event.scaleY < 1.1 && gambar.scaleY < 1){
gambar.scaleX=prevScaleX;
gambar.scaleY=prevScaleY;
}
mat=gambar.transform.matrix.clone();
MatrixTransformer.matchInternalPointWithExternal(mat,internalPoint,externalPoint);
gambar.transform.matrix=mat;
}
gambar.addEventListener(MouseEvent.MOUSE_DOWN, f_begin);
gambar.addEventListener(MouseEvent.MOUSE_UP, f_end);
function f_begin(e:MouseEvent):void
{
gambar.startDrag();
}
function f_end(e:MouseEvent):void
{
gambar.stopDrag();
}
btw your pic is so cute.. :D
Welcome to StackOverflow. I don't usually work with touch events, as I develop software for desktop, not mobile. However, I do have an idea. I'm not going to venture with code, because without any experience writing touch-related event listeners, I'd probably get something wrong. However, I'm pretty confident that the method will work.
For each event in AS3, there is quite a bit of data about it, as you know. One piece of data generally present on mouse- and touch-related events is location - where on the screen or the object the touch occurred.
I would recommend using this data to adjust the location of the picture on the screen, by using the coordinates of the touch event as the new "center" of the view.
Again, I have no code to offer, because I've never written a touch-related event listener before (I'm always working with mouse and keyboard events.) However, I will venture that the likely source of this information, if touch is anything like the mouse, would be found in the e.x and e.y properties within your onZoom function.
I would also venture to say that you could calculate the new position of your picture with an algorithm something like what follows. I'm assuming the picture's registration point is upper left. [THIS IS NOT CODE]
w=picture width
h=picture height
x=picture x position on stage
y = picture y position on stage
tx=touch event x on stage
ty=touch event y) on stage
x = (w/2) - tx
y = (h/2) - ty
Hope this helps!
EDIT: To access the x, y, w, and h on the picture named "picture", simply use the properties picture.x, picture.y, picture.width, and picture.height as your variables.
One other caveat...the x and y on the EVENT may be on the stage, or on the object itself. You'll want to use trace statements and some common sense to figure this out, and may require some more complex math. I don't want to give you TOO much, though, as this is coursework, like you said.
Have a look at GesTouch, it's a great library for touch action
Gestouch Library
Gestouch Examples

How do you add gravity with wall collision in actionscript 3?

I created a script that allows your character to move around with wall collision for every direction. How do I add gravity to it on the y-axis and still have it work the same way (like a platform game)?
I tried something like char.y-- when it hits the platform if it were just the floor, but all that does is slowly bring char up no matter where it is in the object, and once it reaches the top, it shakes like crazy.
A preview of the stage: http://puu.sh/63Y1g.png
//WALL COLLISION
if(walls.hitTestPoint(char.x-(char.width/2),char.y,true) && Key.isDown(Key.A)){
char.x+=5
}
if(walls.hitTestPoint(char.x+(char.width/2),char.y,true) && Key.isDown(Key.D)){
char.x-=5
}
if(walls.hitTestPoint(char.x,char.y-(char.height/2),true) && Key.isDown(Key.W)){
char.y+=5
}
if(walls.hitTestPoint(char.x,char.y+(char.height/2),true) && Key.isDown(Key.S)){
char.y-=5
}
//CHARACTER CONTROLS
char.x+=0;
if(Key.isDown(Key.A)){
char.x+=-5;
speed=-5;
}
if(Key.isDown(Key.D)){
char.x+=5;
speed=5;
}
if(Key.isDown(Key.W)){
char.y+=-5;
speed=5;
}
if(Key.isDown(Key.S)){
char.y+=5;
speed=5;
}
To do this really simply, something like this would suffice:
// Check if there is a floor below the char
if (!floor.hitTestPoint(char.x, char.y + (char.height/2) + 2 , true))
{
// If not, apply Gravity
char.y += 5
}
This will only apply gravity if there is no floor 2 pixels directly below the char (this will stop the jittering, feel free to try with different pixel offsets that might work better in your case), I wasn't sure how you were differentiating between the walls and floor, so replace 'floor' with how you are doing it.
===============
If you would like to add realistic accelerating gravity, you will need to add a variable to track the current vertical speed of the char, then add to that each step
eg. Each step do this instead of just adding to y
// Apply gravity
vSpeed += 5
char.y += vSpeed
But if you are going to do that you might as well start getting into vectors and more complex things, there's a lot of materials on the net for this.

Check If animation is running in cocos2d-x

I am currently learning cocos2D-x and am doing some sprite animation.
My Objective is that when a button is clicked the object moves to left with some animation.
Now if you click multiple times rapidly the animation takes place immediately and it looks like the bear is hoping instead of walking.
The solution to it looks simple that I should check if animation is already running and if running the new animation should not take place.
The following is a part of my code.
CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("AnimBear.plist");
CCSpriteBatchNode* spriteBatchNode = CCSpriteBatchNode::create("AnimBear.png", 8);
this->addChild(spriteBatchNode,10);
CCArray *tempArray = new CCArray();
char buffer[15];
for (int i = 1; i <= 8 ; i++)
{
sprintf(buffer,"bear%i.png", i);
tempArray->addObject(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(buffer));
}
CCAnimation *bearWalkingAnimation = CCAnimation::create(tempArray,0.1f);
startAnimation = CCSprite::createWithSpriteFrameName("bear1.png");
startAnimation->setPosition(ccp (350 , CCDirector::sharedDirector()->getWinSize().height/2 -100));
startAnimation->setScale(0.5f);
startAnimation->setTag(5);
//Animation for bear walking
bearAnimate = CCAnimate::create(bearWalkingAnimation);
Here bearAnimate is a global variable and i wish to know if its currently playing the animation.
How do I do it.?Thank you.
Assume the Sprite that runs the action is
CCSprite* bear;
I think you can use something like
bear->numberOfRunningActions()
numberOfRunningActions( ) returns an unsigned integer, so to check if there are no actions, you would have to check if it returns 0
if ( bear -> numberOfRunningActions( ) == 0 ) {
CCLOG( "No actions running." );
} else {
CCLOG( "Actions running." );
}
The bearAnimate (CCAnimate) has a method to check that.
if (bearAnimate.isDone())
doWhatYouWant();
The method is inherited from CCAction. Good luck.

removing old tiles in a running game made with as3 and flash

I started to build a run game app for Android. I chose to make it in flash using tiles and Adobe Air. The game is that a player should run automatically to the right and avoid some obstacles by jumping or sliding along the ground.
I have made a function which always takes the first level in the array and uses as the starting level.
private function createLevel()
{
map_level.levell();
level = map_level.level1;
for(var t = 0; t < level.length; t++)
{
for(var u = 0; u < level[t].length; u++)
{
if(level[t][u] != 0)
{
var new_tile:platform_tile = new platform_tile;
addChild(new_tile);
new_tile.gotoAndStop(level[t][u]);
new_tile.x = u * 32;
new_tile.y = t * 32;
tiles.push(new_tile);
}
}
}
total_tile_width += u;
}
Then I create a function that takes a random level in the array of paths.
private function random_level ()
This level is then added at the end of the first track when the player has reached a certain length along the track, then the track seems endless and then made such that the camera follows the player.
private function update_level ()
{
random_level();
for(var t = 0; t < mid_lvl.length; t++)
{
for(u = 0; u < mid_lvl[t].length; u++)
{
if(mid_lvl[t][u] != 0)
{
var new_tile:platform_tile = new platform_tile;
level[t][u + total_tile_width] = mid_lvl[t][u];
addChild(new_tile);
new_tile.gotoAndStop(mid_lvl[t][u]);
new_tile.x = (u + total_tile_width) * 32;
new_tile.y = t * 32;
tiles.push(new_tile);
}
}
}
// Indstiller hvis spilleren skal have en stigende fart
if( movementspeed < 40)
{
movementspeed = movementspeed + 2;
}
else
movementspeed = movementspeed;
total_tile_width += u;
trace ("speed: " + movementspeed);
}
All this works as it should and game function also perfect as a PC, but the phone seems quick to overload it, since I can not figure out how to remove the old levels that have already been played and therefore there's going to be a lot levels in the phone memory.
I need to something like removeChild("old tiles the left the stage again) but got no idear how to only find the tiles that old and not just all tiles.
Anyone able to help me? btw hope you understand my question as im not the best at writing english.
Morten
You must use 2 BitmapData objects.
2 are the size of the screen. We call them screenBuffer1 and screenBuffer2
the other is larger than the screen by at least the width of a tile. We call it terrainBuffer
Then :
Init the terrainBuffer by drawing all visible tiles of the start screen
Copy the currently visible area on screenBuffer1
Draw sprites on screenBuffer1
Attach screenBuffer1 to stage
Now we start scrolling
Copy the currently visible area on screenBuffer2, offset by scroll amount
Draw sprites on screenBuffer2
Attach screenBuffer2 to stage
Continue increasing offset and alternate screenBuffer1 and screenBuffer2 so one is visible while you draw on the other
When offset reach the witdh of a tile :
if(offset>tileWidth){
offset-=tileWidth;
// Move content of terrainBuffer to the left by tileWidth pixels
// Draw a new column of tile on the right
}
Then keep on looping !