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

checkPixel issue for Not Phaser.Sprite type #1684

Closed
hightopo opened this issue Mar 21, 2015 · 5 comments
Closed

checkPixel issue for Not Phaser.Sprite type #1684

hightopo opened this issue Mar 21, 2015 · 5 comments

Comments

@hightopo
Copy link
Contributor

In src/input/InputHandler.js checkPixel method, using if (this.sprite.texture.baseTexture.source) for checking Sprite type, but for other type like BitmapText, it has no texture property, we should check BitmapText instance's children for every char Sprite.

So In phaser-examples-master/examples/text/bitmapfont drag.js, if we set bmpText.input.pixelPerfectClick = true; then when we drag text we will get exception, because bmpText has no texture property.

Hope checkPixel method can be improved for supporting check Not Sprite type.

@photonstorm
Copy link
Collaborator

I can confirm this doesn't throw an error in Phaser 2.3.

Equally it doesn't work, i.e. the pixel click is ignored, but it won't crash. So I'll flag this as a feature request for now.

@hightopo
Copy link
Contributor Author

Thanks @photonstorm .
In my project i am doing hittest and checkPixel myself, because i also need to consider cropRect and Text type, and also i change texture.frame's size NOT using scale, so the customized checkPixel code below work right for me.
FYR:

checkPixel = function (game, sprite, localPoint, pixelPerfect, pixelPerfectAlpha) {
    if(!pixelPerfect){
        return true;
    }

    if(pixelPerfectAlpha == NULL){
        pixelPerfectAlpha = 1;
    }

    var anchor = sprite.anchor,            
        texture = sprite.texture,
        cropRect = sprite.cropRect,
        x = localPoint.x,
        y = localPoint.y,
        frame = texture.frame,
        trim = texture.trim,
        crop = texture.crop;

    if (anchor.x !== 0)
    {
        x -= -frame.width * anchor.x;
    }

    if (anchor.y !== 0)
    {
        y -= -frame.height * anchor.y;
    }

    x += frame.x;
    y += frame.y;

    if (trim)
    {
        x -= trim.x;
        y -= trim.y;                
        if (x < crop.x || x > crop.right || y < crop.y || y > crop.bottom)
        {
            return false;
        }
    }

    var hitContext = game.input.hitContext;
    hitContext.clearRect(0, 0, 1, 1);

    if(sprite instanceof PIXI.Text){
        x *= sprite.canvas.width / frame.width;
        y *= sprite.canvas.height / frame.height;
    }else{
        x *= texture.width / frame.width;
        y *= texture.height / frame.height;
    }

    if(cropRect){
        x += cropRect.x;
        y += cropRect.y;
    }        

    hitContext.drawImage(texture.baseTexture.source, x, y, 1, 1, 0, 0, 1, 1);        
    var rgb = hitContext.getImageData(0, 0, 1, 1);
    if (rgb.data[3] >= pixelPerfectAlpha)
    {
        return true;
    }

    return false;

};

@pnstickne
Copy link
Contributor

I think a good approach would be to move the checkPixel out to the game object types and allow polymorphism/specialization. (BitmapText and Text work a bit differently, for instance.)

I briefly looked at such an approach and could not find immediate fault with it.

@photonstorm
Copy link
Collaborator

Agreed, although I don't consider this vital enough for the 2.3 release this week. Something for the roadmap perhaps.

@photonstorm
Copy link
Collaborator

In order to keep the Github issues clear this feature request has been moved to the Phaser 3 wishlist.

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

No branches or pull requests

3 participants