TouchGameView.java

/*
 * Copyright © 2011 Nokia Corporation. All rights reserved.
 * Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation. 
 * Oracle and Java are trademarks or registered trademarks of Oracle and/or its
 * affiliates. Other product and company names mentioned herein may be trademarks
 * or trade names of their respective owners. 
 * See LICENSE.TXT for license information.
 */   

package com.nokia.example.racer.views;

import com.nokia.example.racer.Main;
import com.nokia.mid.ui.gestures.GestureEvent;
import com.nokia.mid.ui.gestures.GestureInteractiveZone;
import com.nokia.mid.ui.gestures.GestureListener;

public class TouchGameView extends GameView implements GestureListener{

    private long lasttimestamp;
    public void gestureAction(Object container, GestureInteractiveZone gestureZone, GestureEvent gestureEvent) {

        switch (gestureEvent.getType()) {
            case GestureInteractiveZone.GESTURE_DRAG:
                long current = System.currentTimeMillis();
                if (lasttimestamp == 0) {
                    lasttimestamp = current;
                }
                if ((current - lasttimestamp) > 20) {
                    handleDrag(gestureEvent);
                    lasttimestamp = current;
                }
                break;
            case GestureInteractiveZone.GESTURE_TAP:
                int x = gestureEvent.getStartX();
                int y = gestureEvent.getStartY();
                if(x <= 60 && y <= 30) {
                    reset();
                    Main.getInstance().showMenu();
                    
                }
                break;
        }
    }

    private void handleDrag(GestureEvent event) {
        int start_x = event.getStartX();
        int Xdistance = event.getDragDistanceX();
        int result = start_x + Xdistance;
        Xdistance = Math.abs(Xdistance);
        if (Xdistance > 5) {
            if (speed > friction) {
                if (result > start_x) {
                    turnRight();
                } else {
                    turnLeft();
                }
            }
        }
    }
}