触摸按下事件,改变图像按钮 [英] Touch Down Event, Change the image button

查看:167
本文介绍了触摸按下事件,改变图像按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道已经有很多类似的问题和答案在这里,我已经做了一些搜索,我想我应该问这是一个新的问题,因为我仍然无法找到正确的答案对我来说。

I know there already are many similar questions and answers here, I've done searching some and I think I should ask this as a new question as I still can't find the right answer for me.

所以,我写了一个简单的纸牌游戏,我得到了90%我所知,从书开始的Andr​​oid游戏第2版马里奥Zechner,不知你们有些人读它。我按照我的写作比赛中指导,我甚至用他在书中(Mr.Nom游戏)提供了框架。我问过他的论坛,但没有任何反应,该论坛是不是太活跃反正。

So, I write a simple card game, and I got 90% of my knowledge from the book "Beginning Android Games 2nd edition" by Mario Zechner, I wonder if some of you have read it. I follow the guide during writing my game, I even use the framework he provides in the book (Mr.Nom game). I've asked in his forum, but no response, the forum is not too active anyway.

这本书从code提供了框架,也有方法来绘制像素图,画线等。所有直接,所以我从来不碰的布局,我也不使用XML。

The book provide framework where there are method to draw Pixmap, draw line, etc.. all directly from code, so I never touch the layout, I don't use XML either.

这本书使用类似于MVC(模型视图控制器)的模型,分离世界和游戏。在presentation和操纵layerd。它分离根据游戏状态的每个UI:准备好,暂停,运行,GAMEOVER。

The book uses model similar to MVC (model view controller), separating the world and the game. The presentation and the manipulation layerd. It separates each UI according the game state: Ready, paused, running, gameOver.

现在我坚持一些code,因为我想改变我的按钮图像。我公司提供2图像(UN pressed和pressed),首先在所谓的(RunningUI)的一部分,我提请联合国pressed形象。并简单地当用户触摸按钮(触摸下来才)我想要的形象改变为pressed形象。并返回到触摸被释放后,再次未pressed形象。

Now I am stuck at some code as I want to change my button image. I provide 2 image (Unpressed and Pressed), at first in so called (RunningUI) part, I draw Unpressed image. and simply when user touch the button (touch down only) I want to change the image to pressed image. And return back to unpressed image again after the touch is released.

private void drawRunningUI() {
    Graphics g = game.getGraphics();
    g.drawPixmap(Assets.buttonUnpressed, 70, 200);
}

该updateRunning code是在这里:

The updateRunning code is here:

private void updateRunning(List<TouchEvent> touchEvents, float deltaTime) {        
    Graphics g = game.getGraphics();
    int len = touchEvents.size();

    for(int i = 0; i < len; i++) {
        TouchEvent event = touchEvents.get(i);
        if (event.type == TouchEvent.TOUCH_UP) {
            if (event.x < 64 && event.y < 64) {
                if (Settings.soundEnabled)
                    Assets.click.play(1);
                state = GameState.Paused;
                return;
            }
        }

        if (event.type == TouchEvent.TOUCH_DOWN) {
            if (event.x >= 120 && event.x <= 180 && event.y >= 250 && event.y <= 380) {   

                Assets.click.play(1);
                g.drawPixmap(Assets.buttonPressed, 70, 200);  <-- nothing happened
            }
        }
    }

    world.update(deltaTime);
}

我修改一些在这里和那里,但我仍然没有得到我想要的结果。

I modified "some here and there", but still I don't get the result I want.

推荐答案

虽然我不知道这个框架,并只能推测在画布上绘画是怎么做的,我怀疑 drawRunningUI()终止后, updateRunning马上叫()和覆盖了previous与 Assets.button pressed (取决于如何无效/重绘处理)。

Although I don't know this framework and can only surmise how the canvas drawing is made, I suspect drawRunningUI() is called immediately after updateRunning() and "overwrites" the previous draw with Assets.buttonPressed (depends on how invalidation / redraw is handled).

无论如何,你应该只画在一个地方,它然后得到一个容易得多。在你的事件处理code,你应该更新UI状态,这你就再使用绘制UI。事情是这样的:

Anyway, you should only draw in one place, and it then gets a lot easier. In your event handling code you should update a UI state which you would then use to draw the UI. Something like this:

private void updateRunning(List<TouchEvent> touchEvents, float deltaTime) {        
    int len = touchEvents.size();

    for(int i = 0; i < len; i++) {
        TouchEvent event = touchEvents.get(i);
        if(event.x < 64 && event.y < 64) {
            if(event.type == TouchEvent.TOUCH_UP) {
                // ...
            }
        else if (event.x >= 120 && event.x <= 180 && event.y >= 250 && event.y <= 380) {
            switch(event.type) {
                case TouchEvent.TOUCH_UP:
                    uiState.updateButtonState(ButtonState.RELEASED);
                    break;
                case TouchEvent.TOUCH_DOWN:
                    Assets.click.play(1);
                    uiState.updateButtonState(ButtonState.PRESSED);
                    break;
                default:
                    // nothing to do
            }
        }
    }

    world.update(deltaTime);

    // I suppose an invalidation has happened / a redraw will happen next
}

private void drawRunningUI() {
    Graphics g = game.getGraphics();
    g.drawPixmap(uiState.getButtonState() == ButtonState.PRESSED ? Assets. buttonPressed : Assets.buttonUnpressed,70,200);
}

这是不够的,但应该说明的想法。

This won't be enough but should illustrate the idea.

这篇关于触摸按下事件,改变图像按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆