Reputation: 197
i am currently working in flashbuilder and i imported a swc wich containes a ratingcontainer. That ratingcontainer contains 5 ratings, they are labeled "Rating1", "Rating2, "Rating3", "Rating4", "Rating5".These are placed on the stage. i also have a sprite called "Star" and when i for example click on "Rating4" then every Rating from 1 to 4 should get stah Star sprite added to them, when i click again on Rating 3 or something then only 3 the first 3 should have the star added. i tried the following:
detailContent.RatingContainer.addEventListener(MouseEvent.CLICK, ratingClickHandler);
private function ratingClickHandler(e:MouseEvent):void{
rating = e.target.name;
rating = rating.replace("Rating","");
for (var i:uint = 1; i==uint(rating); i++){
star = new Rated();
detailContent.RatingContainer.getDefinitionByName("Rating"+e.target.name).addChild(star);
}
but this isnt working at all. Anyone who can help?
Upvotes: 0
Views: 316
Reputation: 9572
Looks like a fairly complicated way to do it.
One option could be to have a Rating class . The Rating class would have a selected & an index variable. When selected, a Star symbol would be visible, if not , then the Star visibility would be false.
The index property would indicate the rating position and would be set when an instance is added to the stage.
In your RatingContainer class, you would have a Vector of Rating objects , each object listening to a click event.
private var ratings:Vector.<Rating> = new Vector.<Rating>();
private function addRatings():void
{
for( var i:int ; i < 5 ; ++i )
{
var r:Rating = new Rating();
r.index = i;
r.addEventListener( MouseEvent.CLICK , ratingClickHandler );
ratings.push( r);
//set the rating position
r.x = 10 * i;
addChild( r );
}
}
private function ratingClickHandler( event:MouseEvent ):void
{
var rating:Rating = event.currentTarget as Rating;
for( var i:int ; i < ratings.length ; ++i )
if( ratings[i].index <= rating.index )
ratings[i].selected = true
else
ratings[i].selected = false;
}
In your Rating class
private var _selected:Boolean;
private var star:Sprite = new Star();
public function set selected(value:Boolean ):void
{
star.visible = value;
_selected = value;
}
Upvotes: 1
Reputation: 1145
First of all, you really should type you variables, like such:
var rating:String = e.target.name;
It makes the code much simpler to read and understand. Second, I'm not sure you need getDefinitionByName here, if Content.RatingContainer is a DisplayObject, you can access the rating sprite like this:
Content.RatingContainer["Rating" + e.target.name]
The problem with the code is that you are concatenating e.target.name to "Rating", so you are actualy looking for the sprite with a name of "RatingRating0" for example. It looks like it should be:
detailContent.RatingContainer.getDefinitionByName("Rating"+rating).addChild(star);
since rating is the id that you appended at the end of the names of the sprites on the stage.
Upvotes: 0