# ActionScript 3

Currently Being Moderated

## Changing frame of object based on position

### Sep 14, 2012 10:33 AM

Is there a way to change the state of an object based on its screen position?  For example, if the object hits X position and Y position, change frame to number 32.  Or something in that vein?  Thanks a lot!

Replies
• Currently Being Moderated
Sep 14, 2012 10:51 AM   in reply to JoeGPcom

Yes, but you would need to continuously monitor for that position being achieved.  If you already have something in place that is continuously monitoring the position, then you could just do a simple conditional within that...

if(objectName.x == someX && objectName.y == someY){

gotoAndStop(32);

}

You probably do not want to try to be so precise with the detection though due to the probablility that you would not be exactly at those positions.

|
Mark as:
• Currently Being Moderated
Sep 14, 2012 11:39 AM   in reply to JoeGPcom

Unless there is some mathematical way of deriving the frame versus position, you will end up needing to have 100's of position checks occuring every time you check a position...

// this line will continuously call the determineFrame function

function determineFrame(evt:Event):void {

if(obj.x > 0 && obj.x <= 20 && obj.y > 0 && obj.y <= 20){

obj.gotoAndStop(32);

} else if(obj.x > 20 && obj.x <= 40 && obj.y > 20 && obj.y <= 40){

obj.gotoAndStop(33);

} else... and on and on... 98 more

}

I kinda doubt you really want to do this if it involves 100 different possible frames, but it's up to you.

If you could determine some mathematical way of deriving the frame number from the position values you'd be much better off, but you'll need some decent math skills to have hope of pulling that off.

|
Mark as:
• Currently Being Moderated
Sep 14, 2012 12:43 PM   in reply to JoeGPcom

Yes, that would be possible if you set the alpha property of the invisible ones to 0 to make them invisible (setting their visible property to false won't work).  Something like...

// this line will continuously call the determineFrame function

function checkForHit(evt:Event):void {

if(obj.hitTestObject(invisibleObj32){

obj.gotoAndStop(32);

} else if(... etc

}

|
Mark as:
• Currently Being Moderated
Sep 14, 2012 4:36 PM   in reply to JoeGPcom

"obj" os the object that you want to have hit invisible things and change its frames.

"invisibleObj32" you got right

bitTestObject() is an Actionscript function that tests if things are overlapping

|
Mark as:
• Currently Being Moderated
Sep 14, 2012 7:32 PM   in reply to JoeGPcom

It is always best to place code on a separate layer.

As far as the errors go, you'd have to show the code for lines 5,7, and 8 to be able to determine what the errors might involve.

If you happened to copy my code example into your project, there is an error in one line...

if(obj.hitTestObject(invisibleObj32){

should be...

if(obj.hitTestObject(invisibleObj32)){

﻿

|
Mark as:
• Currently Being Moderated
Sep 15, 2012 5:32 PM   in reply to JoeGPcom

"... etc" is your cue to fill in the rest (the other 99 of the 100 you mentioned), it is not code

|
Mark as:
• Currently Being Moderated
Sep 15, 2012 6:39 PM   in reply to JoeGPcom

Adding the right brace is correct, though it should be added to line up with the one to the left of the "else"... it closes that section of the conditional...

function checkForHit(evt:Event):void {

if(obj.hitTestObject(invisibleObj32)){

obj.gotoAndStop(32);

} else if(obj.hitTestObject(invisibleObj31)){

obj.gotoAndStop(31);

}

}

I can only guess that the new slew of errors indicate that you have not assigned instance names to the objects on the stage (assuming you have objects on the stage).  Instance names are assigned by selecting the object on the stage and entering a name for it in the Properties panel where it says "<Instance Name>"

The instance names for the objects you have coded for so far include "obj", "invisibleObj32", "invisibleObj31"

If you happen to be using timeline tweening, you need to be sure to assign these names in every keyframe for these objects

﻿

|
Mark as:
• Currently Being Moderated
Sep 16, 2012 4:34 AM   in reply to JoeGPcom

Be sure to assign the names in each keyframe... it sounds like you have 3 keyframes of them.

|
Mark as:
• Currently Being Moderated
Sep 16, 2012 10:48 AM   in reply to JoeGPcom

You can probably make a decision on that after you collect which objects it is hitting by testing which one satisfies the conditions best.  That would mean you'd have to test each object for a hittest (get rid of the "else" aspect), store the objects that register a hit into an array, and then go thru the array to see which meets the criteria the best.

One other way you might be able to work it out while retaining the "else" setup is to order the hittests based on that precedence of positions.

|
Mark as:
• Currently Being Moderated
Sep 17, 2012 5:45 PM   in reply to JoeGPcom

You cannot have two functions with the same name.

Based on what I see of the repetition, that code for both functions can be combined and reduced to the following...

var objects:Array = new Array(obj, obj2);

function checkForHit():void {

for(var i=0; i<objects.length; i++){

for(var n=1; n<22; n++){

if(objects[i].hitTestObject(this["invisibleObj"+String(n)]){

objects[i].gotoAndStop(n);

break;

}

}

}

}

(note: code has not been tested, there may be errors/typos)

﻿

|
Mark as:
• Currently Being Moderated
Sep 17, 2012 6:19 PM   in reply to JoeGPcom

See if you can solve it.  The error is telling you what the problem is, and so could I, but give it a try first.  Compare it to your code from before too when you didn;t have the error.

|
Mark as:
• Currently Being Moderated
Sep 17, 2012 6:57 PM   in reply to JoeGPcom

You're welcome

|
Mark as:
Actions

#### More Like This

• Retrieving data ...