Go Back   Android Forums > Android Development > Application Development
Application Development Dev Lounge for the Coder Folks
Gamers - Check out our new sister sites!
Nintendo Wii U!    |    OUYA - $99 Android System!

test: Reply
 
LinkBack Thread Tools
Old 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
Default 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!

iQue is offline  
Reply With Quote
Sponsors
Old July 6th, 2012, 06:45 AM   #2 (permalink)
Senior Member
 
jonbonazza's Avatar
 
Join Date: Jul 2010
Gender: Male
Posts: 1,938
 
Device(s): Nexus 4, Samsung Galaxy S II, HTC Evo 4G, Amazon Kindle Fire
Carrier: ATT

Thanks: 235
Thanked 468 Times in 290 Posts
Default

If you change initx and inity, you would also need to change the x and y of both _touching Point and _pointerPosition to compensate from the differnce in location.
jonbonazza is online now  
Reply With Quote
Old July 31st, 2012, 07:51 PM   #3 (permalink)
New Member
 
Join Date: Jul 2012
Posts: 1
 
Device(s):
Carrier: Not Provided

Thanks: 0
Thanked 0 Times in 0 Posts
Default

the problem is your bounding box, you hard coded the numbers, you should adjust it to include your initx/inity and just subtract/add width/height. Would be easier if you just used a rectangle class.
Allpowerfool is offline  
Reply With Quote
Reply
Tags
android, development, game, joystick


Go Back   Android Forums > Android Development > Application Development
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 06:55 PM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2013, vBulletin Solutions, Inc.