July 5th, 2012, 02:22 PM
|
#1 (permalink)
|
|
New Member
Thread Author (OP)
Join Date: Jun 2012
Posts: 2
Device(s):
Carrier: Not Provided
Thanks: 0
Thanked 0 Times in 0 Posts
|
android game-dev problem.
Hi!
Im making a game with a sprite that moves thru a virtual joystick, only my joystick is taken from a code example and Ive played with it for a while but the problem is that when I change the location of the joystick on the screen, it stops working correctly, and I cant figure out why. Below follows the code that I use for controlling the sprite with the joystick and how I draw it.
Code:
//variables:
public float initx = 425;
public float inity = 267;
public Point _touchingPoint = new Point(425, 267);
public Point _pointerPosition = new Point(220, 150);
private Boolean _dragging = false;
private MotionEvent lastEvent;
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event == null && lastEvent == null) {
return _dragging;
} else if (event == null && lastEvent != null) {
event = lastEvent;
} else {
lastEvent = event;
}
// drag drop
if (event.getAction() == MotionEvent.ACTION_DOWN) {
_dragging = true;
} else if (event.getAction() == MotionEvent.ACTION_UP) {
_dragging = false;
}
if (_dragging) {
// get the pos
_touchingPoint.x = (int) event.getX();
_touchingPoint.y = (int) event.getY();
// bound to a box
if (_touchingPoint.x < 400) {
_touchingPoint.x = 400;
}
if (_touchingPoint.x > 450) {
_touchingPoint.x = 450;
}
if (_touchingPoint.y < 240) {
_touchingPoint.y = 240;
}
if (_touchingPoint.y > 290) {
_touchingPoint.y = 290;
}
// get the angle
double angle = Math.atan2(_touchingPoint.y - inity,
_touchingPoint.x - initx) / (Math.PI / 180);
// Move the beetle in proportion to how far
// the joystick is dragged from its center
_pointerPosition.y += Math.sin(angle * (Math.PI / 180))
* (_touchingPoint.x / 70);
_pointerPosition.x += Math.cos(angle * (Math.PI / 180))
* (_touchingPoint.x / 70);
// stop the sprite from goin thru
if (_pointerPosition.x + happy.getWidth() >= getWidth()) {
_pointerPosition.x = getWidth() - happy.getWidth();
}
if (_pointerPosition.x < 0) {
_pointerPosition.x = 0;
}
if (_pointerPosition.y + happy.getHeight() >= getHeight()) {
_pointerPosition.y = getHeight() - happy.getHeight();
}
if (_pointerPosition.y < 0) {
_pointerPosition.y = 0;
}
}
else if (!_dragging) {
// Snap back to center when the joystick is released
_touchingPoint.x = (int) initx;
_touchingPoint.y = (int) inity;
// shaft.alpha = 0;
}
return true;
}
public void render(Canvas canvas) {
canvas.drawColor(Color.BLUE);
canvas.drawBitmap(joystick.get_joystickBg(), 380, 220, null);
canvas.drawBitmap(happy, _pointerPosition.x, _pointerPosition.y, null);
canvas.drawBitmap(joystick.get_joystick(), _touchingPoint.x - 26,
_touchingPoint.y - 26, null);
}
initx and inity is where the bg for the joystick is, and if I change these variables + the pointer, the joystick stops working the way it should. But I need to change them since I want to move it :P
So if any1 can help, please do!
also, if you cant help me with that I have the problem that this app crashes when I try starting it in landscape-mode, and since I want to force it into landscape this is a problem, do NOT want portrait for my game. (changing it in manifest to "landscape" makes it crash)
Thank you!
|
|
|