I got a serious problem here: I have a scrolling background that is 1320 x 1000 big. I'm using a sprite as a container where my background canvas is placed inside of it. Additionally I add (via addChild) a score sprite inside of the background sprite (the one that holds the bg canvas so the score appears on top of the bg canvas).
The goal is that when I add a score it shall be stuck to the background (even when the scrollrect moves; it shall have a fixed position on the background).
The problem is that this doesn't work because when the score appears it moves with the scrollrect and this is not what I want. I already tried it with globalToLocal but had no success yet. Probably because I don't really understand globalToLocal (and vice versa). Please can anyone help me out with this problem? Thanks in advance.
These are some code excerpts:
private var backgroundBitmapData:BitmapData = new BitmapData(1320, 1000, false, 0x000000);
private var canvasBitmapData:BitmapData =开发者_JS百科 new BitmapData(1320, 1000, false, 0x000000);
private var canvasBitmap:Bitmap = new Bitmap(canvasBitmapData);
private var canvasRect:Rectangle = new Rectangle(0, 0, 660, 500);
private var backgroundSprite:Sprite = new Sprite();
backgroundSprite.scrollRect = new Rectangle(0, 0, 660, 500);
canvasBitmap.scrollRect = new Rectangle(330, 250, 660, 500);
backgroundSprite.addChild(canvasBitmap);
backgroundSprite.cacheAsBitmap = true;
addChild(backgroundSprite);
tempScoreText = new ScoreTextField(String(10), textFormat, oldAstX, oldAstY, 20);
scoreTexts.push(tempScoreText);
backgroundSprite.addChild(tempScoreText);
I put a sample together to see if I could see what you're experiencing, but I don't think I'm seeing the same thing because this example seems to work like it's supposed to. Here's my example:
var backgroundBitmapData:BitmapData = new BitmapData(1320, 1000, false, 0x000000);
var canvasBitmapData:BitmapData = new BitmapData(1320, 1000, false, 0xff0000);
var canvasBitmap:Bitmap = new Bitmap(canvasBitmapData);
var canvasRect:Rectangle = new Rectangle(0, 0, 660, 500);
var backgroundSprite:Sprite = new Sprite();
backgroundSprite.scrollRect = new Rectangle(0, 0, 660, 500);
canvasBitmap.scrollRect = new Rectangle(330, 250, 660, 500);
backgroundSprite.addChild(canvasBitmap);
backgroundSprite.cacheAsBitmap = true;
addChild(backgroundSprite);
var tempScoreText = new TextField();
tempScoreText.text = "temp score";
backgroundSprite.addChild(tempScoreText);
this.stage.addEventListener(MouseEvent.MOUSE_MOVE, function(event):void {
backgroundSprite.scrollRect.x = mouseX;
backgroundSprite.scrollRect.y = mouseY;
});
If this isn't what you're looking for, maybe you could upload a swf for me so that I could see what you see? Perhaps a SOF chat is in order as well. I've been stuck before and people have helped me out so this is just me paying it forward.
If you want the item to scroll with scroll rect:
stage.addChild(item)
If you want the item to be sticky, then only use
addChild(item)
and use
root.scrollRect
精彩评论