Search code examples
javablackberryblackberry-simulator

Blackberry Touch event detecting object at touch up position


This is my first blackberry app and I am attempting to make a simple game for a touch screen device. Its the typical image split into tiles and you have to unscramble them so the tiles are in the correct order and show the picture. The intention is that the user will be able to "drag" a tile anywhere they want on the screen and they will swap places with the tile they drop it over.

I am using bitmapbuttons for my tiles

    HorizontalFieldManager hfm = new HorizontalFieldManager();
    add(hfm);
    hfm.add(button1);
    hfm.add(button2);
        etc...

I have a class called Puzzle screen as follows:

class PuzzleScreen extends MainScreen implements FieldChangeListener {

I have added the following to try and detect the movement of the persons touch on the screen

protected boolean touchEvent(TouchEvent event) { 
case TouchEvent.MOVE:

            PuzzleTile fieldMove = (PuzzleTile) this.getLeafFieldWithFocus();

            // Dialog.alert("MOVE");

            int[] x_points;
            int[] y_points;
            int[] time_points;
            int size = event.getMovePointsSize();
            x_points = new int[size];
            y_points = new int[size];
            time_points = new int[size];
            event.getMovePoints(1, x_points, y_points, time_points);
                            return true;
        }

        return false;
    }

I need to find which image tile (bitmapbutton) is at the position of the upevent. I have the coordinates (I think) above but not sure how to find which tile that relates to.

Bex


Solution

  • Something like this.

    static class TestScreen extends MainScreen  {
    
        private static String down;
    
        public TestScreen () {
            HorizontalFieldManager hfm = new HorizontalFieldManager();
            add(hfm);
            hfm.add(new TestButton("bt1"));
            hfm.add(new TestButton("bt2"));
            hfm.add(new TestButton("bt3"));
            hfm.add(new TestButton("bt4"));
            hfm.add(new TestButton("bt5"));
            hfm.add(new TestButton("bt6"));
        }
    
        public static void touchDown(TestButton tb) {
            down = tb.getText();
            System.out.println("DOWN in " + tb.getText());
        }
    
        public static void touchUp(TestButton tb) {
            System.out.println("UP in " + tb.getText());
            if(!down.equals(tb.getText()))
                System.out.println("swap " + down + " and " + tb.getText());
            down = "";
        }
    
    
    
    
    class TestButton extends LabelField {
    
        public TestButton (String label) {
            super(label);
        }
    
        protected boolean touchEvent(TouchEvent event) { 
            if(event.getEvent() == TouchEvent.UP)
                TestScreen.touchUp(this);
            else if(event.getEvent() == TouchEvent.DOWN)
                TestScreen.touchDown(this);
           return super.touchEvent(event);
        }
    
    
    }
    
    }