Issue about image sprite

when the program first runs, ImageSprite.collidedwith works fine, but when I restart, Imagesprite.collidedwith doesn't work properly,

Here the code

Here the .AIA file

Game.aia (451.7 KB)

Thank you so much

1 Like

Have you tried debugging the app (e.g. Do Its or Label Debugging) to see exactly which part or exactly which single block has the problem ?

1 Like

From what I saw, you have a persistent traffic pileup at the top of the screen that will happen after a collision, when you drop cars back onto the Canvas randomly without any regard for overlap with other cars.

To avoid this, I suggest keeping a global list of Sprites.

At a collision event, remove both sprites from the board by turning them invisible and disabled, so they can't trigger any events.

At a Clock Timer event, in addition to servicing the Left and Right buttons, also loop through the global sprite list, looking for clearance if it safe to drop another car onto the race course. To be safe, I would consider the course clear if all the enabled cars have their y values larger than the maximum of all possible car lengths.

If the course is clear for an extra car (add extra features like minimum and maximum car counts if you like) then loop through the global car list looking for an eligible (disabled and invisible) Sprite to add back to the course. If you found one, pick an x value for it, and make it visible and enabled.

If this scheme slows the game too much, it could be made more efficient by adding global lists of enabled and global lists of disabled cars, so you could check length of list to see if more cars are needed for the course.

2 Likes

I have done what you ordered, but the car still can't collide even after restarting the game, I have use new clock

New code and AIA file


Game.aia (454.1 KB)

I apologize for the delay, I had many local concerns to deal with, and your project needed simplification and clarification to fit inside my head.

Here is the Designer for Screen2, the action screen:

Notice two changes I made:

  • I swapped out the Sound component for the Player component. The Sound component was not releasing control while it was playing, so the Sprites ignored my changes to their speed and enablement during crashes and my Notifier dialog. Without this change, the other traffic kept going while the crash dialog waited for an answer.
  • I renamed Button1 and Button2 to btnLeft and btnRight.

Global variables:

My suggestion for using two lists of Sprites, Active and Inactive, got too complicated. This was a bad case of premature optimization. Instead, I used value procedures to hide all that.


initialize global Klik to

Initialization:




Left/Right movement:
This is basically unchanged from what you did.




By the way, I renamed the two Clocks to name their purposes.



In ClockLeftRight, I added safeguards to keep the Player car from running off the road, to avoid having to deal with that in another event block.

In ClockTraffic, I gathered up all the logic into procedures and local variables, to keep its workings clear and well defined. Too many global variables increase the worries about side effects, which are a burden.

In the Traffic Clock Timer, we watch for vehicles that are ready to (re)enter traffic.
We only let them into the Canvas if all lanes are clear where they are entering (y=0, the top.)
We use the list of all vehicle Sprites for our decision making.
All vehicles must be out of the entry zone.
A vehicle is out of the entry zone if it is

  • disabled, or
  • has Y > 150, the maximum length of all vehicles.

This procedure returns an available (not Enabled) vehicle Sprite, or ''.

If a vehicle was available, we launch it:


We set its picture randomly, and drop it on a random x coordinate at the top (Y=0 of the Screen.
Other attributes of the Sprite are set, depending on its Picture. I added table lookup functions for width and height based on the Picture, for conciseness and to hide any further picture dependencies.


Traffic replenishment:



Crashes:



I added a pause procedure, to freeze the screen before popping up the Notifier, which I tweaked for clarity:

Screen Management:
I added a procedure, for common code:


when  Screen2 .BackPressed do

The source:
road_game.aia (453.8 KB)
All blocks for Screen2:

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.