Make game less laggy

Hello everyone, I have made a very big app that uses tons of code. Whenever I run the app, it works fine, but it usually crashes when I switch screens. Is this due to the amount of code or just because the app is glitching or another issue.

Thanks, Brady

1 Like

I cannot see your blocks clearly. Please find an empty place in the Blocks editor, right click and “Download blocks as PNG”.

1 Like

Here is standard/good advice,

and how to switch screen

4 Likes

Brady, I strongly advise the use of Virtual Screens. That will stop the screen crash issue and improve on lag. The reason this is so is that Screens are separate allocations of memory (you could think of them as separate Apps as they don't 'know' each other exist). Virtual Screens however share the same memory allocation:

When we define virtual screens, we use one 'real' App Inventor Screen (most often Screen1). Screen-sized Vertical Arrangements on it are displayed/hidden as required - they are the Virtual Screens. This is generally a better approach for multi-screen Apps, they share data without having to "pass" it between screens and it also reduces code duplication, making the App more efficient and the code easier to follow if you have to return to it at a later date.
So, instead of separate "houses", virtual screens are "rooms" of the same "house".

How to capture your Blocks to upload to the Forum:

Right-mouse click in the Blocks work area
download blocks

4 Likes

Here are blocks for 1 screen which has the most blocks out of my whole app if you want other screens let me know.
(For some reason it won't let me make the image any bigger so sorry if its small).

@ChrisWard, I have used that method on other apps, however, it would be very hard and difficult as I already have a working app that I have been working on for a while. I get that method does work and probably has less lag because it doesn't have to load more screens with lots of blocks. It would just be easier if there was an easy fix to make it less laggy and not crash when it switches screens because even though there are lots of blocks, it is a pretty simple app.

-Brady

1 Like

Then again,

and one more

3 Likes

Can you just send different blocks separately or send me the aia file personally.

3 Likes

That would take a lot of time! Click the image to preview, and click it again/scroll to zoom in. The main part is to remove unused parts, and correctly switching screens.

1 Like

should i make this bold so that you can see

3 Likes

No no, that's exactly right - we can click on the image to view it and zoom it :sunglasses:

3 Likes

Here are some suggestions to reduce the amount of code used.

I have seen you use these blocks.

image

To remove redundancy, can you try "if-then, else if-then" statements? If BOSS2 is 1, it can never also be 2.

image

Also, use "for each item in list" block.

image

You can use "if global READYPOWER", since if "global READYPOWER" is true, the next statement can run.

And here:

image

Use "when AnyComponent" blocks.

when any ImageSprite.NoLongerCollidingWith {

if (component = Latter1 OR component = Latter2 OR component= Latter3) {

set CharacterSprite.Y to 35;

}

}
1 Like

It is, and there is no hardship in converting it to Virtual screens. You will at the same time learn better coding practice.

2 Likes

The redundancy, spotted by Gordon_Lu. Something else to notice first about that Block is that you should not need eight Clock Timers. You also do not need four Notifiers. These can be recycled.

A better way to determine the message to display:

Instead of this:

....you can do this:
TidyCode

Note how I have named the image sprite - this is good practice, you will always know what you are looking at if everything is named accordingly. Plus, when you need help on the forum, we need this level to understand your code.

1 Like

One clear reason for crashing @ screen switch is that the Clock Timers are still running - they must be disabled. Of course, that's not an issue when switching Virtual Screens :koala:

How many Screens are there Brady? What does each one do?

Obvious time wasters:

  • Changing Screens

  • Did you read this in an earlier post? My experience with Clocks is some applications need one or several to accomplish a goal. If you use multiple Clocks, ensure that the TimerInterval of the multiple Clocks are each set to different values. For example, if Clock1 fires at 1000 ms, Clock2 should fire at a different value, perhaps 1029 ms. Staggering will help avoid (but not eliminate) collisions (instances when both Clocks fire more or less simultaneously, creating issues). Experiment.

  • Sluggish? Experiment with different TimerInterval settings. What values; sorry I don’t know. What values do you use in the Clock TimerIntervals? Are they staggered?

  • changing Screens (Chris already mentioned this) Before you change (if you must use real Screens) make sure in the Properties AlwaysFires is unchecked (disabled).

  • graphics and redisplaying a screen is one of the worst features of App inventor. AI's graphics are notoriously slow compared to Java and JavaScript based games. AI is not a game engine although some fairly good games can be made.

  • Notifiers. Are they set for long or short duration. Another time waster. Posting to a Label is faster and in some cases a nicer way to provide game info. If you must use them use short duration.

Luck getting rid of your lag. The more sprites, the larger the original graphic image slows your app because the app has to process downsizing the image,

Lagging is a problem with graphic games on App Inventor. The lag can be ameliorated. Hope you find your gremlins. The above list is not exhaustive.

2 Likes

@gordonlu310 thanks for that info, I will try those things you said to make the code shorter and easier.

@ChrisWard, I have 6 screens.
Screen 1: Just has 1 start button and a logo to enter the app.
Screen 2: Lets you pick different levels to play on.
Screen 3, 4, 5: All different levels to play on, one is easy level, one is medium level, one is a hard level.
Screen 6: Credits screen, they get to this after they complete the game.

I will try what all of you said and get back to you, thanks for all your help!

.... They can be one Screen surely? Just populate the Canvas according to level?