I have aa question re:AS 3. I made a Post-It note movie clip that is draggable. I want to create a whole pad of notes, or at least simulate this. I thought the best way to do this would be to add another Post-It whenever the startDrag() is triggered.
I first tried creating the Post-It with symbols but didn’t think I could dynamically create new ones this way. I then created a class and added it to the stage:
…
addChild(new PostItNote());
But I wasn’t able to (or don’t know how) to set the x, y position on the stage.
Anyone have suggestions on the best way to accomplish this or even another way altogether?
EDIT:
package {
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.events.Event;
import flash.text.TextField;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.text.TextFormat;
public class CoinHitTest extends MovieClip {
var count:Number = 0;
var total_count:TextField;
var hitting:Boolean = false;
var coinSnd:Sound = new coin_drop();
var myMoney:TextFormat = new TextFormat();
var noteStack:Vector.<PostItNotes> = new Vector.<PostItNotes>();
public function CoinHitTest(){
for(var $i:int = 0; $i <5;$i++)
{
var newPostIt:PostItNotes = new PostItNotes();
this.addChild(newPostIt);
this.noteStack.push(newPostIt);
}
// constructor code
coin.addEventListener(MouseEvent.MOUSE_DOWN, __handleCoinDown);
coin.addEventListener(MouseEvent.MOUSE_UP, __handleCoinUp);
total_count = new TextField();
total_count.x = 795.20;
total_count.y = 506.15;
total_count.mouseEnabled = false;
myMoney.size = 28;
total_count.defaultTextFormat = myMoney;
total_count.text = String("$" + count);
addChild(total_count);
}
private function __handleCoinDown($evt:MouseEvent):void {
coin.startDrag(true);
coin.scaleX = 1.5;
coin.scaleY = 1.5;
addEventListener(Event.ENTER_FRAME, __checkHit);
}
private function __handleCoinUp($evt:MouseEvent):void {
coin.stopDrag();
coin.scaleX = 1;
coin.scaleY = 1;
removeEventListener(Event.ENTER_FRAME, __checkHit);
}
private function __checkHit($evt:Event):void {
if (this.coffee.hitTestPoint(coin.x,coin.y, false))
{
// do our in-circle check
if((coffee.x - coin.x) * 2 + (coffee.y - coin.y) * 2 <= (coffee.width/2 + coin.width/2) * 2)
{
stopDrag();
coin.scaleX = 1;
coin.scaleY = 1;
removeChild(coin);
coin.x = 116;
coin.y = 380.1;
addChild(coin);
}
}
else
{
trace("Didn't Hit Mug");
}
if (this.coin.hitTestObject(target)) {
if (!hitting) {
coinSnd.play();
count++;
total_count.text = String("$" + count);
coin.stopDrag();
removeChild(coin);
hitting = !hitting;
coin.x = 116;
coin.y = 380.1;
addChild(coin);
coin.scaleX = 1;
coin.scaleY = 1;
}
} else {
hitting = false;
}
}
}
}
EDIT:
package {
import flash.display.MovieClip;
public class PostItNotes extends Sprite{
public function PostItNotes() {
// constructor code
}
}
}
The Problem ist that you set PostItNotes as the BaseClass instead of the Class itself.
That means what you add to the stage is a Class that has only the logic and not the graphic elements.
What you need to do is go back to the symbol in the library -> right click -> properties and change the BaseClass to flash.display.MovieClip (or in your case flash.display.Sprite if you don’t need the Timeline) and set PostItNotes as the Class.
EDIT:
I added the drag action to your PostItNotes class because I think this is where it belongs to as the drag really only concerns the post-it itself.
You probably have to remove the drag code where you had it before.
Do you need help on the creation of the new PostIt too?