1
\$\begingroup\$

I've searched all over the web for an answer to this, and there are simular topics but nothing works for me, and I have no Idea why. I just want to move my sprite using a joystick, since I'm useless at math when it comes to angles etc I used an example, Ill post the code here:

    public float initx = 50; //og 425;
public float inity = 300; //og 267;
public Point _touchingPoint = new Point(50, 300);  //og(425, 267);
public Point _pointerPosition = new Point(100, 170);
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 < 25) {
            _touchingPoint.x = 25; //og 400
        }
        if (_touchingPoint.x > 75) {
            _touchingPoint.x = 75; //og 450
        }
        if (_touchingPoint.y < 275) {
            _touchingPoint.y = 275; //og 240
        }
        if (_touchingPoint.y > 325) {
            _touchingPoint.y = 325; //og 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;
        }
    }
    public void render(Canvas canvas) {
    canvas.drawColor(Color.BLUE);
    canvas.drawBitmap(joystick.get_joystickBg(), initx-45, inity-45, null);
    canvas.drawBitmap(happy, _pointerPosition.x, _pointerPosition.y, null);
    canvas.drawBitmap(joystick.get_joystick(), _touchingPoint.x - 26,
            _touchingPoint.y - 26, null);
}

    public void update() {
    this.onTouchEvent(null);
}

og= original position. as you can see Im trying to move the joystick, but when I do it stops working correctly, I mean it still works like a joystick but the sprite dosnt move accordingly, if I for example push the joystick down, the sprite moves up, and if I push it up it moves left.

can anyone PLEASE help me, I've been stuck here for sooo long and its really frustrating.

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

You use _touchingPoint.x twice in this code but never _touchingPoint.y

 _pointerPosition.y += Math.sin(angle * (Math.PI / 180))
                    * (_touchingPoint.x / 70);
 _pointerPosition.x += Math.cos(angle * (Math.PI / 180))
                    * (_touchingPoint.x / 70);

I think the first instruction must be

_pointerPosition.y += Math.sin(angle * (Math.PI / 180))
                        * (_touchingPoint.y / 70);
\$\endgroup\$
3
  • \$\begingroup\$ hmm.. The thing is it works when its in the original position, but not when I move it, I also thought that line of code was weird but since I dont rly understand the angles I thought Id just let it be. But Ill try that and let you know if it works right away :) ty \$\endgroup\$
    – Green_qaue
    Commented Jul 11, 2012 at 14:49
  • \$\begingroup\$ right, it didnt really solve the problem but it got better, now it moves up and down the way it should, but still no reaction when I move it from left to right. \$\endgroup\$
    – Green_qaue
    Commented Jul 11, 2012 at 14:59
  • \$\begingroup\$ EDIT: I changed the second one to .y aswell and now it works :D I figure since I run my game in landscape maybe it should be y instead of x.. So simple it really frustrating but still awsome ^^ wouldnt have figured it out without your help so thank you very much m8 :) \$\endgroup\$
    – Green_qaue
    Commented Jul 11, 2012 at 15:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .