Move animated movieClips using buttons instead of arrow keys - android

I'm trying to develop a game where I want my character to run when I click a button, and continue running if I hold the button. I'm new to ActionScript 3, so I'm a bit lost here.
I've found code that satisfies my requirements; but it uses the arrow keys, as below:
function moveRunKei() {
if (Key.isDown(Key.RIGHT)) {
dx = 15; //speed
runKei._xscale = 50;
} else if (Key.isDown(Key.LEFT)) {
dx = -15;
runKei._xscale = -50;
} else {
dx = 0;
}
runKei._x += dx;
if (runKei._x < 100) runKei._x = 100; //30
if (runKei._x > 550) runKei._x = 550;
if (dx != 0 && runKei._currentframe == 1) {
runKei.gotoAndPlay("run");
} else if (dx == 0 && runKei._currentframe != 1) {
runKei.gotoAndStop("stand");
}
}
this.onEnterFrame = function(){
moveRunKei();
}
I need to be able to do this using buttons.
////////////////////////////////////////////////////////////////////////////////////
import flash.events.Event;
var mouseDown:Boolean;
var speed:Number=4;
addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
addEventListener(Event.ENTER_FRAME, onEnterFrame);
function onMouseDown(event:MouseEvent):void
{
mouseDown = true;
}
function onMouseUp(event:MouseEvent):void
{
mouseDown = false;
}
function onEnterFrame(event:Event):void
{
if (mouseDown)
{
runKei.gotoAndPlay("Run");
runKei.x += speed;
}
}
This code able to make my character move continuously when I hold the button but it didn't animate while it move(the character freeze until I release the button) - I'm not sure how to explain it.

You'll need to add event listeners for mouse down and mouse up on each of the buttons for movement. Then have booleans that keep track of whether or not the button is down.
It's worth mentioning the code you've linked seems to be actionscript2 so I've changed it to work with as3
var leftDown:Boolean = false;
var rightDown:Boolean = true;
leftButton.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown)
rightButton.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown)
leftButton.addEventListener(MouseEvent.MOUSE_UP, onMouseUp)
rightButton.addEventListener(MouseEvent.MOUSE_UP, onMouseUp)
leftButton.addEventListener(MouseEvent.MOUSE_OUT, onMouseUp)
rightButton.addEventListener(MouseEvent.MOUSE_OUT, onMouseUp)
function onMouseDown(e:MouseEvent):void
{
//since you can't click on two things at once, this is fine.
rightDown = (e.target == rightButton);
leftDown = (e.target == rightButton);
}
function onMouseDown(e:MouseEvent):void
{
//since you can't click on two things at once, this is fine.
rightDown = (e.target == rightButton);
leftDown = (e.target == leftButton);
}
function moveRunKei()
{
if (rightDown) {
dx = 15; //speed
runKei.scaleX = -0.5;
} else if (leftDown) {
dx = -15;
runKei.scaleX = -0.5;
} else {
dx = 0;
}
runKei.x += dx;
if (runKei.x < 100) runKei.x = 100; //30
if (runKei.x > 550) runKei.x = 550;
if (dx != 0 && runKei.currentFrame == 1)
{
runKei.gotoAndPlay("run");
}
else if (dx == 0 && runKei.currentFrame != 1)
{
runKei.gotoAndStop("stand");
}
}
this.addEventListener(Event.ENTER_FRAME, onEnterFrame);
function onEnterFrame(e:Event):void
{
moveRunKei();
}

Related

JavaScript code not working properly on Android

I'm writing a game of checkers with QML, and I have this component which encompasses the player piece and it's mouse area.
When the mouse is pressed, and the user moves the mouse, the cell underneath is highlighted yellow. This and snapping works on Ubuntu, but when I test it on Android, only dragging pieces seems to work.
This is it working on Ubuntu
Here's the code for the player piece:
Component {
id: playerPiece
Image {
id: image
property int cx: 0
property int cy: 0
x: board.x + (cx * window.cellSize)
y: board.y + (cy * window.cellSize)
property int pieceIndex: -1
property int pieceType: 0
MouseArea {
id: ma
anchors.fill: parent
enabled: GameState.playerTurnId == 1 && (pieceType & 1) == 1
drag.target: parent
drag.axis: Drag.XAxis | Drag.YAxis
property int targetCellX: -1
property int targetCellY: -1
onReleased: {
// console.log("released")
if (GameState.lastHighlightedIndex != -1
&& GameState.lastHighlightedY != -1) {
board.children[GameState.lastHighlightedY].children[GameState.lastHighlightedX].children[0].visible = false
if (targetCellX != -1 && targetCellY != -1) {
console.log(targetCellX)
var piece = GameState.playerPieceQMLItems[image.pieceIndex]
console.log(`cx ${targetCellX} cy ${targetCellY}`)
piece.cx = targetCellX
piece.cy = targetCellY
//hack to recalculate positions
board.x++
board.x--
board.y++
board.y--
targetCellX = -1
targetCellY = -1
}
}
}
onPositionChanged: {
if (drag.active) {
// console.log("dragging")
var mousePos = NativeFunctions.globalMousePos()
targetCellX = Math.floor(
(mousePos.x - window.x - board.x) / window.cellSize)
targetCellY = Math.floor(
(mousePos.y - window.y - board.y) / window.cellSize)
if ((targetCellX > -1 && targetCellX < 8) && (targetCellY > -1
&& targetCellY < 8)) {
GameState.tileState[targetCellX + (targetCellY * 8)] = 3
//update the board row column child item
var item = board.children[targetCellY].children[targetCellX]
if (item instanceof Rectangle) {
//remove the highlight from the last highlighted cell
if (GameState.lastHighlightedIndex != -1
&& GameState.lastHighlightedY != -1) {
board.children[GameState.lastHighlightedY].children[GameState.lastHighlightedX].children[0].visible = false
}
item.children[0].visible = true
//console.log(`selected ${GameState.playerPieceQMLItems[image.pieceIndex]}, pieceIndex ${image.pieceIndex}`)
//console.log(`${item.children[1].id}`)
GameState.lastHighlightedX = targetCellX
GameState.lastHighlightedY = targetCellY
}
}
}
}
}
sourceClipRect: {
if (GameState.cf(pieceType, GameState.TS_P1)) {
//king flag set
if (GameState.cf(pieceType, GameState.TS_PK)) {
return Qt.rect(0, 326, 338, 338)
} else {
return Qt.rect(0, 0, 338, 338)
}
} else if (GameState.cf(pieceType, GameState.TS_P2)) {
//king flag set
if (GameState.cf(pieceType, GameState.TS_PK)) {
return Qt.rect(0, 326, 338, 338)
} else {
return Qt.rect(534, 0, 338, 338)
}
}
}
source: "qrc:/pieces.png"
}
}
Which is a part of https://github.com/ben-cottrell-nz/checkers/blob/master/main.qml.
Why is the highlighting and snapping functionality not working when I test my application on Android?
I found out that my method exposed from C++, NativeMethods::globalMousePos was the issue: on Android it was returning -2147483648 for x and y, this was being returned from QCursor::pos.
I changed mousePos in my QML code to use the builtin function mapToGlobal:
var mousePos = mapToGlobal(mouse.x, mouse.y)
Highlighting and snapping to Rectangles works on Android now.

Smooth map rotation in navigation kotlin android

I have problem with smooth map rotation. I use Sensor and onSensorChanged to get direction which side the user is looking at. Then I put it to math degrees (rage -180 : 180)
degres = Math.toDegrees(mOrientation.get(0).toDouble())
and then pass it to other function
if (gps != null) {
if(degres!! !in lastDegres!!-10..lastDegres!!+10 && degres!!>0 || degres!! !in lastDegres!!-10 ..lastDegres!!+10 && degres!!<0 ){
//here
lastDegres = degres
}
}
if statement avoid shaking map when sensor get a little movement.
My problem is what I have type in this if statement to smoothly rotate map. I already trying something like this :
if (gps != null) {
if(degres!! !in lastDegres!!-10..lastDegres!!+10 && degres!!>0 || degres!! !in lastDegres!!-10 ..lastDegres!!+10 && degres!!<0 ){
var result = (lastDegres!! - (degres!! )).toInt()
for (i in 0..result) {
setOrientation(((180 + lastDegres!! + i ).toFloat()))
update()
}
lastDegres = degres
}
}
but it's work only in only right direction, any advices how to allow to rotate smoothly to left and right ?
// ANSWER
if u want use this next step is find closest site left or right to actually position
if (gps != null) {
realpos = degres!! * -1
if (realpos < 0) realpos += 360
dif = if (realpos > lastDegres!!) { realpos - lastDegres!! } else { (lastDegres!! - realpos) }
if (dif > 10) {
if(realpos > lastDegres!!){
for (i in 1..dif.toInt()*50000) {
orientation = (lastDegres!!.toFloat()+i/50000)
update()
}
}else if (lastDegres!! > realpos) {
for (i in 1..dif.toInt()*50000){
orientation = (lastDegres!!.toFloat()-i/50000)
update()
}
}
lastDegres = realpos
}

How do I detect a wiggle motion on Android?

I need an algorithm that can detect a 'wiggle' gesture on Android, and by that I mean where the phone is held in portrait mode and the wrist is twisted left and right, like the motion of opening a round door handle. So I need to capture rotation back and forth around the z axis for a period of time.
A (crude) picture to illustrate the motion I need to capture:
I've tried a number of different algorithms that capture shaking, such as this "Shoogle For Yer Photos" solution seen here on Github, which work quite well, but capture shaking on every axis using the accelerometer. How would I restrict an algorithm like this to only detect rotation back and forth on the Z-axis, 'wiggling' for a minimum of say 4 seconds?
Here is an extract from the 'Shoogle For Yer Photos' code posted above, which shows how they handle shake detection:
private static final long KEEP_DATA_POINTS_FOR = 1500;
private static final long MINIMUM_EACH_DIRECTION = 7;
private static final float POSITIVE_COUNTER_THRESHHOLD = (float) 2.0;
private static final float NEGATIVE_COUNTER_THRESHHOLD = (float) -2.0;
public void checkForShake() {
long curTime = System.currentTimeMillis();
long cutOffTime = curTime - KEEP_DATA_POINTS_FOR;
while(dataPoints.size() > 0 && dataPoints.get(0).atTimeMilliseconds < cutOffTime) dataPoints.remove(0);
int x_pos =0, x_neg=0, x_dir = 0, y_pos=0, y_neg=0, y_dir=0, z_pos=0, z_neg = 0, z_dir = 0;
for(DataPoint dp: dataPoints){
if (dp.x > POSITIVE_COUNTER_THRESHHOLD && x_dir < 1) {
++x_pos;
x_dir = 1;
}
if (dp.x < NEGATIVE_COUNTER_THRESHHOLD && x_dir > -1) {
++x_neg;
x_dir = -1;
}
if (dp.y > POSITIVE_COUNTER_THRESHHOLD && y_dir < 1) {
++y_pos;
y_dir = 1;
}
if (dp.y < NEGATIVE_COUNTER_THRESHHOLD && y_dir > -1) {
++y_neg;
y_dir = -1;
}
if (dp.z > POSITIVE_COUNTER_THRESHHOLD && z_dir < 1) {
++z_pos;
z_dir = 1;
}
if (dp.z < NEGATIVE_COUNTER_THRESHHOLD && z_dir > -1) {
++z_neg;
z_dir = -1;
}
}
if ((x_pos >= MINIMUM_EACH_DIRECTION && x_neg >= MINIMUM_EACH_DIRECTION) ||
(y_pos >= MINIMUM_EACH_DIRECTION && y_neg >= MINIMUM_EACH_DIRECTION) ||
(z_pos >= MINIMUM_EACH_DIRECTION && z_neg >= MINIMUM_EACH_DIRECTION) ) {
lastShake = System.currentTimeMillis();
last_x = 0; last_y=0; last_z=0;
dataPoints.clear();
triggerShakeDetected();
return;
}
}
Any help will be greatly appreciated, thanks.

Unity3D Touch Rotate Object

I would like to rotate my gameobject without using the z-axis, so that it only rotates horizontal and vertical.
Right now I am using this code
void Update () {
if (Input.touchCount == 1) {
var touch = Input.GetTouch(0);
switch(Input.GetTouch(0).phase){
case TouchPhase.Moved:
float swipeDistVertical = (new Vector3(0, touch.deltaPosition.y, 0) - new Vector3(0, startPos.y, 0)).magnitude;
if (swipeDistVertical > 0)
{
float swipeValue = Mathf.Sign(touch.deltaPosition.y - startPos.y);
if (swipeValue > 0 || swipeValue < 0)//up swipe
{
vertical = true;
horizontal = false;
}
}
float swipeDistHorizontal = (new Vector3(touch.deltaPosition.x,0, 0) - new Vector3(startPos.x, 0, 0)).magnitude;
if (swipeDistHorizontal > 0)
{
float swipeValue = Mathf.Sign(touch.deltaPosition.x - startPos.x);
if (swipeValue > 0 || swipeValue < 0)//right swipe
{
horizontal = true;
vertical = false;
}
}
if(vertical)
{
transform.Rotate(touch.deltaPosition.y * 0.3f, 0,0,Space.World);
}
if(horizontal)
{
transform.Rotate(0,touch.deltaPosition.x * 0.3f,0,Space.World);
}
break;
}
}
}
I got this code from this link
Right now I can rotate, but it rotates on the z-axis aswell which I don't want. And it doesn't handle the vertical swipe right, it switches between both instead of recognizing that it is vertical right now.
I use Unity 4.6.2 and this should work on iOS and Android.
Add following code before where you've put "break".
float z=transform.rotation.z;
transform.Rotate(0,0,-z);

Android Google Maps - how to set the zoom level (and center) of map to display all my geoobjects?

I use google api 2.2. How should I set zoom level(maybe center as well) to display all my geo-objects from ArrayList or Array? I should be dependent on landscape/portrait orientation and maybe screen resolution?
I guess that computing min and max for latitude/longitude will not be complicated in your array ;-)
m_Map.getController().zoomToSpan(
maxLatitude() - minlatitude(),
maxLongitude() - minLongitude());
Following is a simple approach I have followed:
1. Find the distance between the two Geo points. Use Loc1.distanceTo(Loc2) to get the distance between them.
2. Based on the distance, you can use the below code to set the zoom level. You may have to improvise on the below code to support all screen sizes.
if (fDistance>0 && fDistance<=0.5) {
iZoomLevel = 18;
} else if (fDistance>0.5 && fDistance<=2) {
iZoomLevel = 17;
} else if (fDistance>2 && fDistance<=3) {
iZoomLevel = 15;
} else if (fDistance>3 && fDistance<=10) {
iZoomLevel = 14;
} else if (fDistance>10 && fDistance<=50) {
iZoomLevel = 11;
} else if (fDistance>50 && fDistance<=100) {
iZoomLevel = 9;
} else if (fDistance>100 && fDistance<=300) {
iZoomLevel = 8;
} else if (fDistance>300 && fDistance<=1000) {
iZoomLevel = 7;
} else if (fDistance>1000 && fDistance<=3000) {
iZoomLevel = 5;
} else if (fDistance>3000 && fDistance<=5000) {
iZoomLevel = 4;
} else if (fDistance>5000 && fDistance<=10000) {
iZoomLevel = 3;
}

Categories

Resources