Phaser inputEnabled Sprite has mouseover even if not on stage - sprite

I found that if I add a Sprite (with inputEnabled = true) to a group and don't add the group to the stage, I can still interact with the Sprite (although it's not shown).
var group = App.phaser.add.group(null, null, false, false);
var bmd = App.phaser.add.bitmapData(100, 100);
bmd.ctx.beginPath();
bmd.ctx.rect(0, 0, 100, 100);
bmd.ctx.fillStyle = '#ff9900';
bmd.ctx.fill();
var sprite = App.phaser.add.sprite(0, 0, bmd);
group.addChild(sprite);
// group.visible = false;
sprite.x = 100;
sprite.y = 100;
sprite.inputEnabled = true;
sprite.input.useHandCursor = true;
The stage is completely blank (which is good). But when I move my mouse over the top left corner I see a mousecursor (and any added event handlers would also respond).
Only way to prevent this form happening is to set the group's visibility to false, but this is clearly not the best solution.
I'm doing somthing wrong or this is a bug in Phaser?

Sprites don't have to be on the display list in order to be interactive, they just have to have 'inputEnabled' set on them. This is commonly used to allow you to create 'invisible' hit areas.
If you want the sprite to be ignored for input you can call sprite.input.stop() and toggle it back on as needed with start.
Also please use Group.add otherwise the sprite doesn't get assigned a z value, throwing it out of sequence in the Group.

Related

Flash CC Canvas and GSAP: How to set registration point of movieclip on stage

How would I go about changing the registration point of a movieclip labeled "tablet" placed on the stage (root) dynamically?
For example: I have the following:
var tablet = this.tablet; //movieclip labeled "tablet" on stage
function resetTablet(){
tablet.regX = tablet.width/2; //move registration point to center
tablet.regY = tablet.height/2;
}
However when I call it using GSAP:
var tl = new TimelineLite();
tl.to(tablet, 1, {alpha:1, onComplete:resetTablet})
.to(tablet, 3, {alpha:0, scaleX:1.5, scaleY:1.5})
the registration point is still set to the upper left corner rather than the center.
What am I doing wrong here? Thanks!
Registration points affect change both the transformation point, but also the position. If you set a displayobject that is 100x100 pixels to regX=50;regY=50, then it will draw from that point, moving the content 50px to the top and left. If you make that change you should also translate the clip to x=50;y=50.
An issue with you example is that there is no width or height on EaselJS content (explained here). You can get the bounds of anything generated by Flash CC using the nominalBounds property, which Flash exports as a property on every object. If you have multiple frames, you can turn on "multi-frame bounds" in the publish settings, and a frameBounds property is added to the objects as well.
Note that nominalBounds and frameBounds are not used by the getBounds method.
Here is how you might be able to approach it.
var bounds = tablet.nominalBounds;
tablet.regX = bounds.width/2;
tablet.regY = bounds.height/2;
// Optional if your actual registration point was [0,0] before:
tablet.x += tablet.regX;
tablet.y += tablet.regX;
Hope that helps.

Fl_Scroll initial scroll values

I'm using FLTK for a project (the version distributed with Debian/sid 1.3.2-6+b1) and I'm having some trouble initializing Fl_Scroll's content scroll values.
I create a Fl_Double_Window and on the right side a vertical panel using Fl_Scroll, it is positioned at left x 600 and top y 24.
Then I set Fl_Scroll's type to Fl_Scroll::VERTICAL and place a Fl_Button inside, everything works fine.
The problem? Fl_Scroll initialize already scrolled with xposition() = 600 and yposition() = 24 (X and Y of the Fl_Scroll's constructor?? Is supposed to work so?), instead I want it to initialize with content scrolled at left and top 0, 0, so I tried scroll_to() in different places, before and after window's show(), on FL_SHOW of the subclassed Fl_Scroll (I didn't really need to subclass Fl_Scroll, just trying to get scroll_to() working), also on overridden draw() after
the parent draw(), even creating Fl_Scroll at 0, 0 then position() at 600, 24, but nothing.
The only way I got scroll_to() working is if I call it from an async event after the application initialize (FL_KEYUP).
Here's the overridden Fl_Double_Window window constructor:
group = new Fl_Group(0, 0, WIN_W, WIN_H);
{
menu = new Fl_Menu_Bar(0, 0, WIN_W, MENU_H);
menu->add("File/Quit", FL_CTRL+'q', cbMenuQuit_i);
glview = new CGLView(0, MENU_H, WIN_W-PROPS_W+1, WIN_H-MENU_H);
group->resizable(glview);
scroll = new CScroll(WIN_W-PROPS_W, MENU_H, PROPS_W-1, WIN_H-MENU_H);
scroll->type(Fl_Scroll::VERTICAL);
{
Fl_Pack *pack = new Fl_Pack(0, 0, PROPS_W-1, WIN_H-1);
{
btnAddLayer = new Fl_Button(0, 0, PROPS_W, 32, "##+ Add Layer");
btnAddLayer->callback(btnAddLayerCb_i, (void *)this);
}
pack->end();
}
scroll->end();
}
group->end();
end();
size_range(WIN_MIN_W, WIN_MIN_H); /* Make the window resizable */
show();
Never mind, got it, two altenative:
create Fl_Scroll at 0, 0 then position() it after scroll->end()
create Fl_Scroll at the wanted position then after scroll->end() use negative values scroll_to(-X, -Y)
In both cases is important that the widgets contained are already added (scroll_to() alter cotained widgets coords)
Isn't clear to me why X and Y of Fl_Scroll's constructor are used.

Dragging a circle

I am beginning to use snap.svg, and stuck in a probably simple question. I draw a circle, and want to let the user drag it around, and when the user stops dragging, I want to know the alert the final position of the circle.
I started with this:
var s = Snap("#svg");
var d = s.circle(20,20,5);
Then I created the following functions as drag event-handlers; I only need the drag-end event so I made the two other event-handlers null:
var endFnc = function(e) {
alert("end: "+e.layerX+","+e.layerY);
}
var startFnc = null;
var moveFnc = null;
Then I installed the drag event handlers:
d.drag(moveFnc,startFnc,endFnc);
Whenever I tried to drag a circle, the end event fired, but, the circle did not move.
So I figured maybe I have to also create the drag move event (although I don't need it):
var moveFnc = function(dx, dy, x, y) {
this.transform('t' + x + ',' + y);
}
Now the circle did move, but, not exactly at the mouse, but approximately 20 pixels at its bottom right.
I read the documentation here: http://snapsvg.io/docs and there seems to be no explanation about how to create working drag events.
How does anyone get this knowledge?!
After struggling for some hours to do this with snap.js, I finally discovered svg.js and its draggable plugin, with which it is so much easier:
var draw = SVG('svg');
var circle = draw.circle(10).attr({cx:30,cy:30,fill:'#f06'});
circle.dragend = function(delta, event) {
alert(this.attr('cx'))
}
circle.draggable();
So, I switched to svg.js ...

Any way to get a HaxeFlixel group to clear out?

There is a too long, didn't read version down below.
So I've been making a little game in which the player has to click on a grid of bricks that matches the color of the needed brick in the upper right hand corner of the screen. After they click on the needed color, the bricks explode and the bricks of the same color next to them explode as well creating combos. That leaves holes in the grid so I have to somehow reset the grid itself without resetting the gamestate itself. I've got something working right now which is this:
private function ResetNow():Void
{
if (Restter == 1) Restter = 0;
//if this block is up here, same results
/*
wantedBricks.kill();
wantedBrik._changeColor = FlxMath.rand(0, 2);
bricks.autoReviveMembers = true;
bricks.revive();
*/
Restter = 0;
//Removes stray detectors so the neverending combo bug won't occur
for (stray in dets.members) stray.kill();
if (Restter == 0)
{
wantedBricks.kill();
wantedBrik._changeColor = FlxMath.rand(0, 2);
bricks.autoReviveMembers = true;
bricks.revive();
wantedBricks.autoReviveMembers = true;
wantedBricks.revive();
for (zgem in bricks.members) zgem.EQUITYCHECK = FlxMath.rand(0, 2);
}
//add(bricks);
Restter = 1;
}
So, again, I have a grid of blocks set up at create, that is group bricks. And I have a sprite in the upper right corner which is wantedBrik. What happens during gameplay, is the player clicks on the bricks that matches the wanted bricks to clear them out of the grid. When there are no more wantedBricks(a group), it is supposed to reset the grid, and change the color of the wantedBrik. I also have it somewhere else in the code that if a member of the big grid's EQUITYCHECK(basic object hacked in value) is equal to the wantedBrik, add it to the wantedBricks(which, is why I'm checking for no more of them). So, what happens?
Well, if the color of the wantedBrik doesn't change, everything's fine and resets like normal. the wantedBricks group acurately counts the bricks that actually match the wantedBrik's color. And when it does change, for some reason, gameplay is normal. BUT, wantedBricks not only thinks that the old color is still needed, but it also thinks the new color is still needed too. So when the player clicks the new needed color bricks, they do explode, but because wantedBrik thinks the old color is still wanted, it doesn't hit null and the grid won't reset.
What can I do to make sure that wantedBricks behaves correctly after a color change?
TL;DR version: I need to get a Haxe array to forget or lose old numbers. How can I do this?
The "Pirate Pig" sample may be useful to you. Since it is a puzzle game, there may be some similar problems that were solved there. You can find it using openfl create or nme create depending on which you are currently using.
You can create a simple array like this:
var myArray = [];
You can also type arrays, like this:
var numbers = new Array<Float>();
Then you can use push(), concat() and other array methods.

SVG - raphael: storing last path selected so element data can be changed

The current state
As from my link you can pick different regions on the map and everything seems to be working until you re-select a county you have selected before. Data values stored with each path decide if it isSelected or notSelected. I have no problem in changing the element data just clicked with this but I can't find a way of storing the last element selected in a way that I can change it's element data. Which means I first have to click on the previous county to set it's element data to notSelected
First I define var currentcountyselected = "";. This allows me to store the paths[arr[this.id]].name;. When I click on a new path I can make the last path fill change with $('#'+currentcountyselected).attr({fill: attributes.fill});
In Raphael's for loop I set obj.data('selected', 'notSelected'); so all path elements are set to notSeelected.
So what I need is some way to store the last path so I can change it's element data
This is the click function cleaned up from live example.
obj.click(function(){
if(this.data('selected') == 'notSelected')
{this.animate({fill: '#698B22' }, 300);
this.data('selected', 'isSelected');
$('#'+currentcountyselected).attr({fill: attributes.fill});
paths[arr[this.id]].value = "isSelected";
currentcountyselected = paths[arr[this.id]].name;
}
else
{this.animate({fill: '#32CD32'}, 300);
paths[arr[this.id]].value = "notSelected"; /* set path value*/
this.data('selected', 'notSelected');
}
});/* end mark selections */
I've been working on this project for a while and the client now wants the interface to work differently. This has really ate up my hourse.
EDIT:Although I have found a solution by simply taking out the if/else I would still like to know how to get at element data in a previous path (or any path for that matter).
Here is my solution, posted as it might help someone. The link in my question has problems with click happy users.
Globals
var previouscountyselected = "Mayo"; /* default start, can be any county(path) */
var start = true;
var past = null;
Changed code
obj.click(function(){
if(paths[arr[this.id]].value == 'notSelected')
{
this.animate({fill: '#698B22'}, 200);
paths[previouscountyselected].value = "notSelected";
paths[arr[this.id]].value = "isSelected";
previouscountyselected = paths[arr[this.id]].name;
if (!start && past != this)
{
past.animate({ fill: '#fff' }, 200);
}
past = this;
start = false;
}
else if(paths[arr[this.id]].value == 'isSelected')
{
this.animate({fill: '#32CD32'}, 200);
paths[arr[this.id]].value = "notSelected"; /* set path value */
}
});
Overview
if (!start && past != this) is a little unusual and is required or animated fades get messed up and choppy. The fade is not triggered if it is the first time a path is clicked and if you just hammer clicks on one path it doesn't fade to white. The main if/else handles the actual control value.
Until I get a jsfiddle up this link will demonstrate the desired behaviour.
Note! the drop menu in this link does not work.
Click happy friendly

Resources