[SOLVED] Scrolling (scrollRect) not working on Flash target

I there !
Playing with scrolling, I meet a strange behavior : scrollRect works great on Neko/Html5/C++, but not on Flash target !

I tried applying it on the Main sprite, and on a “scene” child sprite, and had the same result.
Works perfect on Neko/Html5/C++, not at all on Flash - no effect at all.

Here is a sample of my Camera class :

class Camera extends Rectangle {

    public static var scene:Sprite;

    public function new(scene_:Sprite){ // triggered by Engine.new()
        super(0, 0, Lib.width, Lib.height);
        scene = scene_;
        
        // hop ceci devient le viewport !
        scene.scrollRect = this;
    }

    public function setPosition(){}
    public function moveBy(x_:Float, y_:Float=0){    
        x += x_; y += y_;
        for (i in 0 ... fixedSprites.length) {
            if(fixedSprites[i] != null) fixedSprites[i].moveBy(x_, y_);
        }
    }
}

On Flash it’s kind of fun, my fixed children Sprites go right slowly, while the viewport doesn’t change at all.
You may watch it happen here :
Flash
Html5 (works as wanted)

And here’s my config :

haxe 3.2.0
neko 2.0.0
hxcpp: [3.2.102]
lime: [2.4.1]
openfl: [3.0.6]
spritesheet: [1.2.0]
tweenx: [0.0.5]

I’m glad it works fine with Html5, though, it has been a serious issue with HaxePunk on a previous project, that’s why I’m building my tools with openfl directly.
It’s kind of fun to meet that kind of issue on the historical target of OpenFL !

Hmm, Flash scroll rects are sort of funny, I’m trying to think if there’s a good way to make it behave. It uses Adobe’s implementation, so it really isn’t something we have any control over :cry:

public function moveBy(x_:Float, y_:Float=0){    
    x += x_; y += y_;
    for (i in 0 ... fixedSprites.length) {
        if(fixedSprites[i] != null) fixedSprites[i].moveBy(x_, y_);
    }
    #if flash
        scene.scrollRect = this;
    #end
}

Yes !
So Flash forgets the scrollRect every frame, and you just’va got to remind him onEnterFrame.
I’ll keep that in mind.

Thanks @player_03, it works great with this little hack, problem solved !

Nope.

When you set scene.scrollRect = this, Flash makes a copy of your Rectangle object. It remembers this copy instead of yours, so that changing yours won’t change Flash’s. If you stop calling scene.scrollRect = this, the scroll rectangle will stay where it is.

There’s probably a reason for this. Perhaps the DisplayObject needs to be notified when there’s a change so that it can be redrawn. Or maybe it’s just to annoy you; I wouldn’t know.

Ok.
At first scene.scrollRect is null, at least on Neko, that’s why I started by initiating it.

So I thought maybe doing

scene.scrollRect.x += x_; scene.scrollRect.y += y_;

on enter frame would work on every target, as I would address the flash copy of my rectangle object…
But nope, flash doesn’t seem to remember it (Neko and Html5 do).

But it’s fine with tour hack, so I’ll go with it !

I have a feeling this stuff has to do with the Flash renderer caching things

Flash also makes a copy when you access scene.scrollRect. The code would look something like this if it was written in Haxe:

private var _scrollRect:Rectangle;
public var scrollRect(get, set):Rectangle;

private function get_scrollRect():Rectangle {
    if(_scrollRect == null) {
        return null;
    } else {
        return _scrollRect.clone();
    }
}

private function set_scrollRect(value:Rectangle):Rectangle {
    if(value == null) {
        _scrollRect = null;
        return null;
    }
    
    if(_scrollRect == null) {
        _scrollRect = new Rectangle();
    }
    
    _scrollRect.copyFrom(value);
    return value;
}

You can test it if you don’t believe me:

scene.scrollRect = this;
trace(scene.scrollRect == this); //false
trace(scene.scrollRect == scene.scrollRect); //false

@player_03 don’t worry, I’m not questionning what you said, and your solution works perfectly :wink:

I just felt I could be smart and find a more elegant fix by myself…
But nope !

The process you describe is surprising, I can’t see why Flash would do that… but there’s more important : your fix works !
Thanks for that, and for your explanation.