Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strange behavior of OnTouched #298

Open
nighca opened this issue Jul 3, 2024 · 2 comments
Open

Strange behavior of OnTouched #298

nighca opened this issue Jul 3, 2024 · 2 comments
Labels
documentation Improvements or additions to documentation

Comments

@nighca
Copy link

nighca commented Jul 3, 2024

For sprite A with code:

// sprite A
onTouched => {
    println "onTouched"
}

The callback will not be called when A collides with another sprite (referred as B).

The callback is called when A collides with B, and method Touching of B is called with A, for example:

// sprite B
onMoving => {
    if touching("A") {
        // do whatever here
    }
}

It's strange, but may be intended. @xushiwei can you offer any hint?

Related PR: #68

P.S. There is no similar API in Scratch

@xushiwei xushiwei added the documentation Improvements or additions to documentation label Jul 3, 2024
@xushiwei
Copy link
Member

xushiwei commented Jul 3, 2024

The background of onTouched event is that the two sprites A and B need to have the same understanding of the world: for example, A is a bullet and B is a monster. When A touches B, A will disappear and B will lose health. We want to avoid the situation where A disappears but B does not lose health, or B loses a lot of health (repeatedly checking that it was hit by a bullet). To this end, we let B receive the onTouched event when A detects that it is touching B.

@nighca
Copy link
Author

nighca commented Jul 5, 2024

Consider details about the process of "touching" between A & B, it can be devided into several stages:

  1. The first time the engine found that there is an overlap between A & B, referred as touchstart
  2. There is a period, in which A & B keep the overlap, refered as touching
  3. The first time, afterwards, the engine found that the overlap disappears, referred as touchend

If we define event touching, which is expected to be fired repeatedly in the 2nd stage, it causes problems like

B loses a lot of health (repeatedly checking that it was hit by a bullet)

But if we provide event touchstart for such case, the problems can be avoided.

Actually in my opinion, in most cases developers care about touchstart more than touching or touchend, because most of the effect of touching / collision should take place when the touching / collision starts.

For the other problem:

A disappears but B does not lose health

It should be considered an implementation bug of the engine. Whatever the event is (touchstart, touching, touchend, etc.), when the event is detected by the engine, it should be the engine's responsibility to ensure that the event is broadcasted to all attendees. When A touches B, B should always be notified of that, even when A is notifed first and destroyed itself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

2 participants