Displaying object in random x-position within the Screen & Changing object speed

243
September 11, 2017, at 1:35 PM

I have been creating this simple game for fun, and I have ran into to some troubles.

1st Problem: Displaying an object within the screen width, I have checked other answers in stack and applied seems to not work.

txtWord is set as wrap_content, though it would be easier if it was set to a particular size, but words have different lenghts so I kept it wrapped.

This is my Mainactivity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_arcade);

 frameWidth = frame.getWidth();
    boxSizeX = txtWord.getWidth();
    randomWord = randomGenerator.nextInt(916);
    randomWordToDisplay = easyWords[randomWord];
    txtWord.setText(randomWordToDisplay);
    //PROVIDE RANDOM X LOCATION
    newXposition = r.nextInt(frameWidth);
    if (newXposition > frameWidth - boxSizeX)
    {
        newXposition = frameWidth - boxSizeX;
    }
    txtWord.setX(newXposition);
    }

2nd Problem: Setting new speed on an object falling down. As the score rises for the user, I woudl want to change the speed and increase it little by little. But what happens is that the current speed just adds with the speed I want to increase so it because more faster.

This is my Activity:

   public void startTimer() {
    timer = new Timer();
    initializeTimerTask();
    timer.schedule(timerTask, 100, 20);
}
public void stoptimertask() {
    if (timer != null) {
        timer.cancel();
        timer = null;
    }
}
public void initializeTimerTask() {
    timerTask = new TimerTask() {
        public void run() {
            handler.post(new Runnable() {
                public void run() {
                    goDown();
                }
            });
        }
    };
}
public void goDown() {
    frame = (FrameLayout) findViewById(R.id.frame);
    frameHeight = frame.getHeight();
    boxY = (int) txtWord.getY();
    boxSizeY = txtWord.getHeight();
    if(scoreCount >= 0)
    {
        boxY += 1;
    }
    if(scoreCount >= 5)
    {
        boxY += 2;
    }
    if(scoreCount >= 10)
    {
         boxY += 3;
    }
    txtWord.setY(boxY);
    if (boxY > frameHeight - boxSizeY) {
        boxY = frameHeight - boxSizeY;
        stoptimertask();
        displayGameOver();
    }
}

Thanks in advance for any insight or help! :D

Rent Charter Buses Company
READ ALSO
Get 2 selected value and pass to button for display

Get 2 selected value and pass to button for display

What's the best way for me to get both selected value from the 2 selection list and display the image from an array once the user hit the button? Is it possible?

308
Populating Expandable Listview with SQLite database

Populating Expandable Listview with SQLite database

I have an SQLite database that has the columns ID, Homework, Classname, dateI also have an expandable listview

343
Firebase Admob Crashes [AGAIN]

Firebase Admob Crashes [AGAIN]

This is the third time Admob with Firebase decided to crash, so there's a lot of reused information

309
Netty UDP receiver fails on Android but not on desktop

Netty UDP receiver fails on Android but not on desktop

I have a Netty implementation class that works fine on the desktop but fails on AndroidI'm very confused

486