#mxgraph : Retrieving Rotation value of a cell - mxgraph

I'm trying to retrieve rotation value of currently selected cell, during and after rotation with handle.;
the cell "style" seems to hold the rotation value in its string but I did not find the way to retrieve it even with mxUtils.getValue(style, "rotation",0);
var cell = graph.getSelectionCell();
var preview = this.graphHandler;
// during mouse move
if (preview != null && preview.shape != null) {
var angle = preview.shape.rotation; }
else {
var angle = mxUtils.toRadians(mxUtils.getValue(cell.getStyle(),mxConstants.STYLE_ROTATION, 0));
// var angle = cell.state.shape.getShapeRotation();
}
Any help would be helpful
Philippe

Try mxUtils.getValue(graph.view.getState(cell).style,mxConstants.STYLE_ROTATION,0)

Related

how do I call a function if the image clicked is equal to its matching word? Actionscript3

When my random word appears the user has to memorise it and click the correct corresponding image to it. I'm trying to write the code that runs if the user selects the right image. I have paired my words and images in my array. I'm just unsure as how to go about calling this function.
This is what I've attempted so far, but this isn't working. I'm new to actionscript3 so excuse the lack of knowledge as I am trying to teach myself.
All help greatly appreciated!!
This is one way you can do this:
See code comments
basket.visible = false;
//------ Home Button ------\\
backhome1btn.addEventListener(MouseEvent.CLICK, goback1Click);
function goback1Click(event:MouseEvent):void{
gotoAndStop("homepage");
}
//-------------------
var score:int = 0;
var items:Array = new Array(); //store all food items in array
var wordsToShow:Array = new Array(); //store all words to show in array - this is the array that will keep track of which has been asked (or rather not asked yet)
//to reduce redundant code, call this with each food item (below)
function initFoodItem(item:MovieClip, word:String):void {
item.word = word; //forget the array, just store the word on the image as a dynamic property
item.addEventListener(MouseEvent.CLICK, foodClicked);
items.push(item); //add to array
wordsToShow.push(item); //add to array
item.visible = false;
}
initFoodItem(oc, "Orange Juice");
initFoodItem(sand, "Sandwich");
//...repeat for all other food items
//now randmize the words to show array:
wordsToShow.sort(function(a,b):int {
return(Math.random() > .5) ? 1 : -1;
});
var curAnswer:MovieClip; //a var to store the current correct answer
//this does the next question per se
function displayWord():void {
if(wordsToShow.length < 1){
//they've all been asked
gotoAndPlay("gameoverpage");
return;
}
curAnswer = wordsToShow.pop(); //assigns the last item in the array to the cur asnwer, and pop also removes that item from the array (so it won't get asked again)
randomword.text = curAnswer.word; //assign the text to the word value of this item
randomword.visible = true;
remember.visible = true;
}
remember.addEventListener(MouseEvent.CLICK, readyClick);
//when you click your ready button
function readyClick(e:MouseEvent):void {
//we have an array of all items, let's loop through it and change them all to be visible
for (var i:int = 0; i < items.length; i++) {
//items[i].alpha = 1; //alpha values are 0 - 1 in AS3
items[i].visible = true; //use visible instead of alpha if just toggling visibility, it's more efficient
}
randomword.visible = false;
remember.visible = false;
bask.visible = true;
notepape.visible = false;
//! another reason to use visible instead of alpha = 0, is alpha = 0 items will still be clickable! visible = false items cannot be clicked.
}
function foodClicked(e:MouseEvent):void {
if (e.currentTarget == curAnswer) {
//if the current target (the item clicked) is the same item as what we stored in curAnswer, you answered correctly, so do this:
score += 10; //or however much you get for answering correctly
gotoAndStop("listpage"); //not sure what this does?
displayWord();
}else {
//wrong
gotoAndStop("gameoverpage");
}
}

How to check if user clicked on smaller object before larger object?

Hey Everyone so I am currently working on a game and the objective is for the user to click on objects on the stage. But the user has to click on the largest objects first before the user clicks on the smaller objects. I wanted to make it that if the user clicks on a smaller object first and not the larger one then the game will be over. I thought I could go about setting this up with booleans for each object on the stage but my if statements arent cutting it here is how I have it set up:
Here are my objects and booleans I use:
//Add box references
public var box_1:MovieClip;
public var box_2:MovieClip;
public var box_3:MovieClip;
public var box_4:MovieClip;
//Booleans
private var b1:Boolean;
private var b2:Boolean;
private var b3:Boolean;
private var b4:Boolean;
now I set all the Booleans to false and if the user clicks on one of the objects I set the Boolean to true.
Here are my if statements tied to my main ENTER_FRAME Listener:
private function level_1():void
{
if (b1)
{
mainScreen.box_1.gotoAndPlay("B1GONE");
b1 = false;
}else
if (b2)
{
mainScreen.box_2.gotoAndPlay("B2GONE");
b2 = false;
}else
if (b3)
{
mainScreen.box_3.gotoAndPlay("B3GONE");
b3 = false;
}else
if (b2 && !b1)
{
endGameCondition();
}
}
On this statement:
if (b2 && !b1)
{
endGameCondition();
}
I was trying to state that if box_2 is true meaning that its clicked on and box_1 hasnt been clicked on yet which is the larger object then the game is now over due to the user not clicking on the largest object first. I have it setup to where box_1 is the largest object and the others are the next size down.
Can anyone see why this isnt working correctly or if there is a better method in doing this?
**** UPDATE HOW MY MAIN CLASS IS SETUP NOW **************
Where I add all my Movie clips and variables:
public class boxTapEngine extends MovieClip
{
//Screens
private var mainScreen:mcMainScreen;
//Add box references
public var box_1:MovieClip;
public var box_2:MovieClip;
public var box_3:MovieClip;
public var box_4:MovieClip;
private var aBoxesArray:Array;
in my constructor function:
aBoxesArray = [box_1, box_2, box_3, box_4];
//AddMainScreen
mainScreen = new mcMainScreen();
mainScreen.x = (stage.stageWidth / 2);
mainScreen.y = (stage.stageHeight / 2);
stage.addChild(mainScreen);
//Initiate numbers
nLevel = 1;
for each(var box:MovieClip in aBoxesArray)
{
box.addEventListener(MouseEvent.CLICK, boxClick);
}
finally on the boxClick function:
private function boxClick(e:MouseEvent):void
{
var box:MovieClip = e.currentTarget as MovieClip; //get a reference to one that was just clicked
box.mouseEnabled = false; //we'll use this as a flag to know if it's been clicked yet
box.gotoAndPlay("BGONE");
//check to see if previous boxes have been clicked.
//this will iterate through all the boxes in order
for (var i:int = 0; i < aBoxesArray.length; i++)
{
if(aBoxesArray[i] == box) return; //if we've reached the currently clicked box, all is good, no need to keep checking so let's exit this loop and function
if (!aBoxesArray[i].mouseEnabled)
{ //one of the previous boxes hasn't been clicked yet
endGameCondition();
}
}
}
Probably isn't working because your final else if statement won't ever be reached (because you're handling b2 == true earlier on which will then bypass all other else statements). Plus your setting b2 to false when you handle it earlier, so it will always be false by the time it gets your final statement.
You need to move that final else if before you check for the other things. See code comments:
//Do this first, and not as a else if
if (b2 && !b1){
endGameCondition();
return; //no need to check checking things if it's game over
}
//now you can do the rest
if (b1)
{
mainScreen.box_1.gotoAndPlay("B1GONE");
b1 = false;
}else
if (b2)
{
mainScreen.box_2.gotoAndPlay("B2GONE");
b2 = false;
}else
if (b3)
{
mainScreen.box_3.gotoAndPlay("B3GONE");
b3 = false;
}
As an aside, you don't need the enter frame handler, you can just check everything on click. Something like this would work:
var boxes:Array = [box_1,box_2,box_3,box_4]; //make sure this is in order of what needs to be clicked first
//if you wanted to actually sort them dynamically based off total size (regardless of what order you've stuffed them in the array), you could add in something like this, which will order them from biggest to smallest:
boxes.sort(function(a,b){
//compare which item has a greater total area (width * height)
if(a.width * a.height > b.width * b.height) return -1; //A is bigger, so put a before b in the array
if(a.width * a.height < b.width * b.height) return 1; //put b before a in the array
return 0; //return 0 if they are the same
});
for each(var box:MovieClip in boxes){
box.addEventListener(MouseEvent.CLICK, boxClick,false,0,true);
}
function boxClick(e:Event):void {
var box:MovieClip = e.currentTarget as MovieClip; //get a reference to one that was just clicked
box.mouseEnabled = false; //we'll use this as a flag to know if it's been clicked yet
box.gotoAndPlay("BGONE");
//or you could just do this to get rid of the box:
if(box.parent) box.parent.removeChild(box);
//check to see if previous boxes have been clicked.
//this will iterate through all the boxes in order
for(var i:int=0;i<boxes.length;i++){
if(boxes[i] == box) return; //if we've reached the currently clicked box, all is good, no need to keep checking so let's exit this loop and function
if(!boxes[i].mouseEnabled){ //one of the previous boxes hasn't been clicked yet
endGameCondition();
}
}
}
//Store your clips in an array
var myClips:Array = [mc1,mc2,mc3,mc4];
//get the clip sizes and store it to an array.
var sizes:Array = new Array();
for (var i:uint = 0; i < myClips.length; i++) {
sizes.push(myClips[i].width);
}
//apply Numeric array sort.
sizes.sort(Array.NUMERIC);
function onClickAction(e:MouseEvent):void {
//Check wheather the array is empty or not.
if (sizes.length != 0) {
//Check wheather the clicked object bigger or not.
if (e.target.width == sizes[sizes.length - 1]) {
trace("Bigger");
e.target.alpha = .5;
sizes.splice(sizes.length-1,1);
} else {
trace("Smaller");
}
}
}

Having a lot of trouble detecting collisions between animated shapes in KineticJS

I have one rectangle (rectangle) that is fixed on its x-axis but moves up its y-axis when clicked and moves down its y-axis when released.
I have a setInterval function that creates instances of rectangles (name) that are fixed on their y-axis and are animated to move from right to left across the stage. The y-values are randomly generated in the setInterval function.
I've tried the following functions:
var collisionDetection = function(a,b){
var status = false;
var boardX = 350;
var boardY = a.y();
var attackX = b.x();
var attackY = b.y();
if(boardX == attackX){
console.log('true');
}
}
collisionDetection(rectangle,name)
And:
function doObjectsCollide(a, b) {
if( !(
((a.y() + a.getHeight()) < (b.y())) ||
(a.y() > (b.y + b.getHeight())) ||
((a.x() + a.getWidth()) < b.x()) ||
(a.x() > (b.x() + b.getWidth()))
));
console.log('true');
};
doObjectsCollide(rectangle,name)
Neither of these have worked. I can't tell if it's because instances of the rectangle are created in a setInterval function which makes it some kind of scoping issue, or it's because the x/y values are constantly changing, but collision and animation go hand-in-hand so I don't understand why that would be a problem.
Any help in the right direction would be hugely appreciated.
Try this slightly more efficient refactoring:
Demo: http://jsfiddle.net/m1erickson/hS9P4/
function doRectsCollide(a,b){
var ax=a.x();
var ay=a.y();
var bx=b.x();
var by=b.y();
return(
!(
(bx>ax+a.width()) ||
(bx+b.width()<ax) ||
(by>ay+a.height()) ||
(by+b.height()<ay)
)
);
}

How to check Google map overlay type

Is that possible to check the type of a google map overlay.
var polygon = new G.Polygon();
var rectangle = new G.Rectangle();
var circle = new G.Circle();
var shape;
Now, my code will dynamically assign these overlay to shape variable.
But how can I detect or check the type of an overlay named shape? I can't find the solution using Google.
You can use instanceof, but I'd advise against it. It's not part of the API and could break any time in the future.
It would be better to assign an attribute upon initialization.
var polygon = new G.Polygon();
polygon.type = 'polygon';
var rectangle = new G.Rectangle();
polygon.type = 'rectangle';
var circle = new G.Circle();
polygon.type = 'circle';
console.log(shape.type);
You can check the class via the Javascript instanceof operator:
var polygon = new G.Polygon();
var rectangle = new G.Rectangle();
var circle = new G.Circle();
var shape = selectShape(polygon, rectangle, circle); // your dynamic selection funktion
if (shape instanceof G.Polygon) {
alert("found Polygon");
}
I wrote a function that returns the type of shape it was given. It supports circle and polygon but I'm sure someone can figure out how to add rectangle. It simply checks for unique properties of each.
function typeOfShape(shape){
if(typeof shape.getPath === "function"){
return "polygon";
}else if(typeof shape.getRadius === "function"){
return "circle";
}else{
return "unknown";
}
}

Quickly Determining the Position of Data in an Array

I have a data structure as the image below illustrates. I need to quickly figure out the index of the cells to the right or left of the highlighted cell group.
You can see in the code below I am naively looping through ALL cells at every index to determine if there is a cell at the requested index. This works great when I have a few (hundred) cells, but breaks down quickly when I have thousands of cells.
In this particular case the highlighted group is mobile, and can only move to the index before or after the previous/next occupied cell. So groupMinX/maxX is the minimum and maximum x value it can move based on the position of other cells in the row.
private var movingGroup:CellGroup; //selected group
public function getCellAtIndex(index:int):ICell
{
for each(var cell:ICell in cells)
{
if(cell.index==index)
return cell;
}
return null;
}
public function groupMinX(xPos:Number):Number
{
var index:int = xPos/cellSize;
var cellsOnLeft:Array = getAllCellsOnLeft(index-1);
if(cellsOnLeft.length > 0)
return cellsOnLeft[cellsOnLeft.length-1].x + cellSize;
return 0;
}
public function groupMaxX(xPos:Number):Number
{
var index:int = xPos/cellSize;
var cellsOnRight:Array = getAllCellsOnRight(index);
if(cellsOnRight.length > 0)
return cellsOnRight[0].x;
return (maxIndex)*cellSize;
}
private function getAllCellsOnLeft(ofIndex:int):Array
{
var index:int = 1;
var cells:Array = [];
while( ofIndex >= 0 )
{
var cell:ICell = getCellAtIndex(ofIndex);
if(cell && !movingGroup.containsCell(cell))
cells.unshift( cell );
ofIndex--;
}
return cells;
}
private function getAllCellsOnRight(ofIndex:int):Array
{
var index:int = 1;
var cells:Array = [];
while( index <= maxIndex )
{
var cell:ICell = getCellAtIndex( ofIndex + index );
if(cell && !movingGroup.containsCell(cell))
cells.push( cell );
index++;
}
return cells;
}
What I am looking for is an efficient method for scanning/tracking the cells. The array I am looping through doesn't actually contain the blank cells, but it has the cells with the index property.
Whoops in my tweet I added the wrong link. Use a linked list. Have your 'Cell' class implement a Linked List Node interface. No need to loop or use conditionals.
http://en.wikipedia.org/wiki/Linked_list
As the list is ordered, I would suggest you do a binary search to find your wanted cell. Then, rather than looping through the elements to the left and the right, simply slice the array to form two new arrays of the left and right side.
Something like so, parhaps? (please excuse any syntactical errors, I don't know actionscript...)
private function search(array:Array, index:int, low:int, high:int) :int
{
if (high < low)
return -1
var middle:int = low + ((high - low) / 2)
if (array[middle].index > index)
return search(array, index, low, middle - 1)
else if (array[middle].index < index)
return search(array, index, middle + 1, high)
else
return middle
}
private function sliceBitsOff(index:int)
{
var index:int = search(yourArray, 7, 0, yourArray.length-1)
var rightArray:Array = yourArray.slice(0, index - 1)
var leftArray:Array = yourArray.slice(index + 1, yourArray.length)
}
You could just pre-cache the indices of the cell on the left and the cell on the right ...
Your choice of a data structure is non-ideal. Please describe what you are trying to do at a higher level. Was this data structure given to you, or did you come up with it?
I'm not sure if I am missing something here but if this is a numerically index array of objects (cells),
I think you could do something like...
cellsArr[cellsArr.indexOf(cellObj1) - 1] // previous cell
cellsArr[cellsArr.indexOf(cellObj2) + 1] // get the cell after a "highlighted" cell