flame-engine / flame

A Flutter based game engine.
https://flame-engine.org
MIT License
9.29k stars 913 forks source link

Render order between parent component and its children #678

Closed pedia closed 3 years ago

pedia commented 3 years ago

Current bug behaviour

Parent render above its children.

Expected behaviour

Parent should render under its children.

Steps to reproduce

class A extends SpriteComponent {
  Future<void> onLoad() async {
     sprite = Sprite('parent.png');
     addChild(SpriteComponent(sprite: 'child.png'));
  }
}

Then parent.png render above child.png. Is this right? Affected components are: SpriteComponent and SpriteAnimationComponent normally.

Flutter doctor output

[✓] Flutter (Channel master, 1.27.0-2.0.pre.79, on macOS 11.0.1 20B50 darwin-x64, locale en-CN)
[✓] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
[✓] Xcode - develop for iOS and macOS
[✓] Chrome - develop for the web
[✓] Android Studio (version 3.5)
[✓] IntelliJ IDEA Community Edition (version 2019.1.3)
[✓] VS Code (version 1.53.2)
[✓] Connected device (2 available)

• No issues found!

More environment information

[✓] Android :android:
[✓] iOS 🍎
[✓] Web :browser:

flame-version: 1.0-rc6

Now:

class SpriteComponent {
void render(Canvas canvas) {
    super.render(canvas);

    sprite?.render(
      canvas,
      size: size,
      overridePaint: overridePaint,
    );
  }
}

should be:

class SpriteComponent {
void render(Canvas canvas) {
    // render children
    sprite?.render(
      canvas,
      size: size,
      overridePaint: overridePaint,
    );

    // render self
    super.render(canvas);
  }
}
spydon commented 3 years ago

Good find! Your comments are swapped though? (// render children < - > // render self) Do you want to make a PR for this?

pedia commented 3 years ago

Yes, comments should be swapped.

Sorry, I changed the code, but it's not worked properly. I think there are more bugs in position of children component related to its parent(translate).

spydon commented 3 years ago

How are you positioning your components? Are you aware that the children are supposed to be positioned in relation to their parent, and not to the absolute top left corner?

pedia commented 3 years ago

Yes, children's position is relative to their parent.

spydon commented 3 years ago

So what is the problem that you are having with the positioning @pedia?

spydon commented 3 years ago

I realized we have to do this differently, since prepareCanvas is called in super and it should always be called first.

pedia commented 3 years ago

@spydon Yes, I test a little This commit fixed. But it's ugly. Maybe there is better solution.

spydon commented 3 years ago

@spydon Yes, I test a little This commit fixed. But it's ugly. Maybe there is better solution.

Yeah, it would be nicer to not have all subclasses having to run prepareCanvas

spydon commented 3 years ago

@pedia now it is solved, we'll release the fix with rc7 which should be released in the coming days (maybe even today)

pedia commented 3 years ago

Great,👍👍👍

spydon commented 3 years ago

It is now released, please try if it works like you expect it to