Question

I am a game dev beginner and I am currently having trouble with my game.... I would like the game to update the canvas each time I loop in order to show my character move in a single press of an image... Invalidate() does not work for me here since it only displays the end position of my character Here is the code for it:

    @Override
public boolean onTouchEvent(MotionEvent event) {
    final int x = (int)event.getX();
    final int y = (int)event.getY();
    int action = event.getAction();

switch (action) {
    case MotionEvent.ACTION_DOWN:

            if (rollbounds.contains((int)x, (int)y))
            {
                //dice roll
                Random rand = new Random();

                move = rand.nextInt(6) + 1;

                //end of dice roll
                //move counting

                for (int loop=move;loop>0;loop--)
                {
                if ((charypos >= 220)&&(charxpos == 9))
                {
                    Toast toast = Toast.makeText(getContext(), "You Win", Toast.LENGTH_SHORT);
                    toast.show();
                    loop=1;
                }
                if (charxpos != 205&&charLeftMove==false)
                {

                    for (int loop2 = 28;loop2>0;loop2--)
                    {
                        charxpos+=1;
                        //update or invalidate here

                    }

This here is a board game where when I touch the roll button, I would get the character to move the number of spaces smoothly, since invalidate causes it to move to the end position instantly. Help is greatly appreciated

Était-ce utile?

La solution

Do you know the touch event is actually in UI thread? Even if you called the invalidate, the canvas will not draw anything until your whole code block has been executed. That means the system will render the UI after your code

for (int loop=move;loop>0;loop--)

that's why you found always move to the end position. You should move your code to case MotionEvent.ACTION_MOVE. Don't use loop in an event. just make calculation once by every touch event. For example, globally declare a variable, and increase value by touchEvent

int a = 0;

public boolean onTouchEvent(MotionEvent event) {
    final int x = (int)event.getX();
    final int y = (int)event.getY();
    int action = event.getAction();

switch (action) {
    case MotionEvent.ACTION_MOVE:
                ++a;
                invalidate();
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top