简体   繁体   中英

Bridge building algorithm in Java?

I am making a bridge building game for Android. As you know there are two coordinates for drawing a line. Firstly, When we push "put" button and select a dot then it's the first coordinate of the line, secondly the coordinates where we keep touching on screen is always displayed as the second coordinate, lastly, where we release our finger is decided as the second coordinate of the line. And there will be more than one lines. I'd be glad if anyone explain these to me.

You will have to override the onTouchEvent function of the respective activity:

@Override
public boolean onTouchEvent(MotionEvent event) {
    int action = event.getActionMasked();
    float x = event.getX();
    float y = event.getY();
    if (action == MotionEvent.ACTION_DOWN) {
         // save the coordinates somewhere
    } else if (action == MotionEvent.ACTION_UP) {
         // save the coordinates as well
    } else if (action == MotionEvent.ACTION_MOVE) {
         // display the coordinates
    }
}

Then you simply have to use the stored coordinates to draw a line between the coordinates, eg within a canvas that is located on your activity.

You will find a sophisticated example here: http://www.vogella.com/tutorials/AndroidTouch/article.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM