5 star rating system in actionscript 3 - actionscript-3

i need to build a 5 star rating for a image gallery. when someone rates a image from the gallery i need to retrieve the rating (say like .5 or 2.5 or 3 or 4.5 or 5)
How can we create something like a 5 star rating system in AS 3.0?
Edited:
not the solutions to send the rating vlues to database... I am looking for the logic to set the rating (stars changing the color indicating the rating has been updated) and get the rating values applied...

Now that the question has completely changed, lets look at displaying some stars:
You can use the Polygon tool to create stars. Select this tool and then in properties, select Options and then Star.
Draw 5 stars on a layer.
Convert these to a MovieClip.
Place them on their own layer. Copy them and then place them onto another layer.
Draw a rectangle that will be the color you want to fill the stars with large enough to cover all of the stars. Put this on another layer and then mask that with one of the star layers. Convert the rectangle to a MovieClip so you can tween it.
Create a tween of the rectangle moving from the left to the right so that it slowly covers the stars.
You should have something like this:
Use gotoAndStop() to stop on a desired frame. In my example, 50 is a full 5-star rating. 25 would be a 2.5 star rating and fill 2 and a half stars. Etc.
Old Answer
I'm keeping this here purely for the sake of people searching for things like "rating system in AS3" which will most likely require the below info.
You'll need to store the ratings in a database. The concept I use (and probably that everyone else uses) is that you store records containing a voter, the rating they assigned and what they voted on eg:
Marty | 3 | hat.jpg
John | 5 | hat.jpg
Simon | 1 | tasty-cereal.jpg
You then request all of the records that relate to a given image, combine all of the ratings found and then divide the total you get by the amount of ratings given. This will yield the average which will represent a rating out of 5.
As an example, the rating for hat.jpg could be found like this in PHP:
<?php
$score = 0;
$query = mysql_query("SELECT ratings.score FROM ratings WHERE image='$image_from_flash'");
while($row = mysql_fetch_assoc($query))
{
$score += $row["score"];
}
$average = $score / mysql_num_rows($query);
echo $average; // This will be sent back to AS3.
?>
To send and receive the ratings, you will need to use URLLoader to communicate with server-side technology, like PHP. This will talk to the database, work out the average as I mentioned above and then send back to AS3 the average rating for you to use.
If you need information about how to work with PHP + AS3 and send information between the two, that's an entirely different question that will require a very detailed answer. You're better off Googling for information about this.
Let me get you started:
Send and load variables with AS3 and PHP

You may want to think about it as a volume slider. for example:
you have a a movie clip of 5 stars that is 100px in width. you make a mask that is 100px wide to cover up the stars. Set up an event listener for MouseEvent.CLICK on the stars movieclip and make the width of the mask whatever the mouseX is relative to the the total width of the clip. something like:
var relativeMouseX:int = mouseX - stars.x; // you'll get a number between 0-100px
so if the value is say 67px then you can do this operation:
67/100 = .67 * 5 = 3.35;
3.35 stars would be the rating.
starsMask.width = relativeMouseX; //makes the mask's width reflect how many stars is showing.

Related

Keeping the current CCSprite to another scenes

I'm a beginner of using cocos2d-x.
My problem is I dun know how to keep the CCSprite to another scenes.
The details of my case:
I've made a class"Scene01"includes 5 characters CCSprite with attributes, each of them class name like C1,C2...C5.
I've made a "Draw" button at class"Scene02"to draw out 1 of them randomly. I put this action at "CCTouchesBegan"...the character draw setting as below:
if (probability >0 && probability <=20) {result = C1::create();}
else if (probability >20 && probability <=40){result = C2::create();}
...until C5::create();
I use "this->addChild(result);" display on "Scene02" at "CCTouchesBegan".
But I don't know how to keep the generated "result(CCSprite)" to the new scene class"Scene03". Is there any better way(s) to simplify my case or any method(s) can help me to complete it?
You could try the following: retain the Sprite, remove it from Scene02 (keeping it on the heap) and then add it to the Scene03.
//(Scene02)
result->retain();
result->removeFromParent();
..
//(Scene03)
this->addChild(result);
result->release();

game - how can I drag objects (cars with numbers) into targets (start line) AS3.0.?

I am having this problem, where I have several cars, numbers and letters, and need to put 5 cars in the starting places. -random order is ok.
I' having trouble finding in AS3 a way so that the EndX and EndY of each object can be in the starting lines and be considered right no matter the order!
I'm having trouble putting the code here so, heres a titanpad with the code:
this is the code:
being (um, dois, tres, quatro) the movieclip instance name for each numbered car.
https://titanpad.com/42vtnCbvLu
First of all, you could probably benefit by using the distance between two points formula and seeing if the distance is less than a certain value rather than checking in all 4 directions manually:
Math.abs(Math.sqrt((x2-x1)^2 + (y2-y1)^2))
Let the position of the car be (x1,y1) and the start position (x2,y2).
This formula will give you the distance between the two points in any direction, and you could test maybe whether this value is less than your offset.
As for the cars in any order part, I'm interpreting that you have your cars and you want the user to drag them to one of 5 spots, a bit like this:
spot1
spot2
spot3
spot4
spot5
All with respective coordinates. My suggestion would be to have a boolean flag for whether each spot is occupied that stops the program checking whether a car is put there after it has been taken once.
Once all these flags are true, then you can proceed.
Hope this helps.

AS3. How to change character's template in adobe flash cs5 using AS3?

I'm creating flash game. Here will be abillity to choose one of two (or more) character's. So I have in library created symbol hero. It have 7 animations on click (moving, jumping, attacking etc..)
So I want to create something like hero 2, that player could choose which one likes more. Just how to do that? Create new layer in hero and add animations or how?
I'm asking that because in Action Script 3 I'm adding hero in this case and It always will add the same:
private function create_hero()
{
addChild(Hero);
Hero.gotoAndStop("stay");
Hero.x = stage.stageWidth/2;;
Hero.y = ground.y - 60;
Hero.x_speed = 0;
Hero.y_speed = 0;
}
Maybe here is abillity to make something like that layer2.addChild(Hero);?
Or I need to create new symbol hero2? I don't like this idea, because I have long code to control hero, so for every character I'll need to dublicate code. Could you help me? Thank you.
The proper way is to dynamically create an instance Hero at game start based on the game player's selection. You indeed create two (or more) symbols in Flash CS, with common frame labeling (you can use different animation lengths for different heroes), then, once you've got your hero selection (hero1,hero2,hero3 etc, regardless of their amount) you get the class name from selection and get your Hero variable to be assigned an instance of the respective class. An example:
static var heroes:Array=[hero1,hero2,hero3]; // note: symbol names!
var Hero:MovieClip; // note, not "hero1" or anything, but general MovieClip type
public function selectHero(what:int):void {
// this is called with correct "what", design yourself. I use array index
var whatHero:Class = heroes[what]; // get selected hero symbol
if (Hero && Hero.parent) Hero.parent.removeChild(Hero);
// clean up previous hero. Drop listeners here, if any
Hero = new whatHero(); // get new hero
// process as usual, don't forget to "addChild(Hero)" somewhere
}
How this works: First, you give the player a hero selection dialogue, and call selectHero with proper value of what (0 for hero1, 1 for hero2, etc, as you make your heroes array). Then, the whatHero variable is assigned the corresponding class (yes, one can assign classes to variables in AS3!) And then the class gets instantiated via new construction, and the resultant movie clip is then assigned to Hero variable. After this is done, you can use your hero as before.

How to make a button add random numbers from 0-9 into two textfields? | Flash cs6 -as3

There are two textfields and one button on the scene; txtNumberOne, txtNumberTwo and btnEnter.
When I click btnEnter I want to add a random number from 0-9 (restricted) into both of the text fields (both will be randomized, not duplicated).
If the numbers are the same, the button will dissapear(hide), and if the numbers are either lower or greater than eachothers, the button will still be there. Can someone help me with the basic coding of this? I've recently started learning as3, and this is one of my tasks. Thank you.
pseudocode, since you're learning and the core of that is to do it yourself:
fun roll(click) {
n1 = int(random(10))
n2 = int(random(10))
text1 = string(n1)
text 2 = string(n2)
if (n1 == n2)
hideButtons()
}

How to do slow motion effect in flash game with box2d

As i am working on top view racing game in which am trying to add slow motion effect when a car hits objects. I have tried with decreasing Stage.frameRate but the game appears lagging. and i have also tried with online tutorial called touch my pixel ( ref : http://blog.touchmypixel.com/2009/12/box2d-contactpoint-filtering/ ). But i didn't understand.
Is there any solution for showing such kind of slow motion effect. can anybody help me in this regard
Thanks and regards,
Chandrasekhar
Easiest way would be to have a global modifier property somewhere which can be used to multiply the movement of everything in the game.
For example, you could have the property speedModifier default to 1.
public var speedModifier:Number = 1;
And whenever you apply velocities, just multiply by the modifier:
body.SetLinearVelocity( new b2Vec2(x * speedModifier, y * speedModifier) );
This way all you need to do to half the speed of the game is to half the modifier:
speedModifier = 0.5;
To keep your code tidier and make managing this component of your game easier, there is probably a straightforward way to iterate over all of the bodies within the Box2D world and modify their velocities at the top of each update step. Something along the lines of:
for each(var i:b2Body in world.GetBodyList())
{
var currentVel:b2Vec2 = i.GetLinearVelocity();
var newVel:b2Vec2 = new b2Vec2(
currentVel.x * speedModifier,
currentVel.y * speedModifier
);
i.SetLinearVelocity( newVel );
}