• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

hitTest globalToLocal or other

Community Beginner ,
Oct 19, 2023 Oct 19, 2023

Copy link to clipboard

Copied

Hello!
I'm trying to count how many turns the yellow dot (this.area_mc.yellowDot_mc) makes before reaching the center or edge.
The yellow dot goes from the edge to the center and then back. I placed a vertical rectangle (rect_mc) and tried to identify the number of turns made with hitTest, but I don't think I was able to make the coordinate transformation work. The rectangle can be either inside area_mc or outside, but the yellow dot must be inside the area. I tried all localToGlobal, localToLocal, globalToLocal methods but I was unsuccessful. link to the .fla file: https://drive.google.com/file/d/1UPHJDcEc6sps58bExh6dNnwDuAXLwLJ8/view?usp=sharing


I thank you for your help.

 

print.PNG

TOPICS
Code , How to

Views

194

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 19, 2023 Oct 19, 2023

Copy link to clipboard

Copied

what's this supposed to do

 

createjs.Ticker.setInterval( tempoDeMovimento ); /*tempo de atualização*/

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 20, 2023 Oct 20, 2023

Copy link to clipboard

Copied

This is the 200 ms time interval listener that calls the fn_Spin( ) function that increments and decrements the values for sine and cosine variations, causing the yellow dot to move in both directions.

/*update time do dot move */

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 20, 2023 Oct 20, 2023

Copy link to clipboard

Copied

not much is saved by converting to polar.  you can just check the angle (and, if you find it easier, you can change the angle and angle_inc variable names to the ones you used):

 

 
var rect_mc = this.area_mc.rect_mc;
var area_mc = this.area_mc;
var yellowDot_mc = this.area_mc.yellowDot_mc;
 
var tempoDeMovimento = 20; // milissegundos para o listener 
var angle = 0;   
var angle_inc = .1;  //para incemento ser igual ao índice
var seno;
var cos;
var circRadius = 300; /*raio máximo*/
var flagSentido = 'reduzindo'; /*direção sentido centro ou borda*/
var contarChamadasDeMovimentos = 0;
var contarVoltas = 0;
var count = 0;
 
createjs.Ticker.addEventListener("tick", fn_Spin);
createjs.Ticker.setInterval( tempoDeMovimento ); /*tempo de atualização*/
 
function fn_Spin(event) {
seno = Math.sin ( -angle ) * circRadius;
cos  = Math.cos ( -angle ) * circRadius;
 
// new code
angle = (angle+.1);
if(angle>=Math.PI*2){
angle=0;
count++;
console.log(count);
}
 
if ( circRadius < 0 ) {
flagSentido = 'crescendo';
contarVoltas = 0;
console.log(count);
count = 0;
console.log('< 0 ')
} else if ( circRadius > 300 ) {
flagSentido = 'reduzindo';
contarVoltas = 0;
console.log(count);
count = 0;
console.log('> 300 ')
}
 
if ( flagSentido == 'crescendo' ) {
circRadius = circRadius + .1;
} else if ( flagSentido == 'reduzindo' ) {
circRadius = circRadius - .1;
}
 
yellowDot_mc.x = seno;
yellowDot_mc.y = cos;
};

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 20, 2023 Oct 20, 2023

Copy link to clipboard

Copied

worked perfectly. Simple and effective. thank you very much!!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 20, 2023 Oct 20, 2023

Copy link to clipboard

Copied

LATEST

you're welcome.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Oct 20, 2023 Oct 20, 2023

Copy link to clipboard

Copied

A suggestion: Why don't you convert the coordinates of the yellow point to polar coordinates and compare θ to some predefined value? In this way, it will be clear when the point crosses a given "meridian", despite of the distance to the pole.

 

 

 

function cartesianToPolar( centerX, centerY, x, y ){
	var dx = x - centerX;
	var dy = y - centerY;
	var r = Math.sqrt( dx * dx + dy * dy );
	var theta = Math.atan2( dy, dx );
	return { r:r, theta:theta };
}​

 

 

 



- Vlad: UX and graphic design, Flash user since 1998
Member of Flanimate Power Tools team - extensions for character animation

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 20, 2023 Oct 20, 2023

Copy link to clipboard

Copied

Your solution is very elegant. Congratulations and thank you!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 20, 2023 Oct 20, 2023

Copy link to clipboard

Copied

Hi.

 

If you want to stick to the collision detection approach, you just need to change this line:

var pt = yellowDot_mc.localToLocal(0, 0, rect_mc); // instead of ...(yellowDot_mc.x, yellowDot_mc.y, rect_mc);

 

This method expects internal coordinates.

 

Regards,

JC

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 20, 2023 Oct 20, 2023

Copy link to clipboard

Copied

in general, a hittest isn't going to work for this unless you add constraints and are careful to ensure exactly one + hittest per 2 pi revolution of the dot.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines