how to remove an object with as2 on a frame # - actionscript

i'm using as2. How do i delete these snowfalkes on a certain frame? i'm creating the snowlfakes using an array on frame 40. I want to get rid of the frames around 60.
var snowflake:Array;
for (var i:Number=0;i<100;i++) {
snowflake[i]=new Snowflake(this);
}
import Snowflake.*;
class Snowflake {
public var _snowflake:MovieClip;
private var _ranSnowflake:Number;
private var i:Number;
private var k:Number;
private var rad:Number;
private static var NUM_SNOWFLAKE_TYPES:Number=7;
private static var MOVIE_WIDTH:Number=590;
private static var MOVIE_HEIGHT:Number=390;
private static var FALLING_SPEED:Number=30;
private static var WIND_SPEED:Number=5;
private static var ROTATION_SPEED:Number=4;
function Snowflake(container:MovieClip) {
this._ranSnowflake=Math.round((Math.random()*Snowflake.NUM_SNOWFLAKE_TYPES)+1);
this._snowflake=container.attachMovie("snowflake"+this._ranSnowflake,"snowflake",container.getNextHighestDepth());
this._snowflake._x=(Math.random()*Snowflake.MOVIE_WIDTH);
this._snowflake._y=0;
this._snowflake.parent=this;
this.i=1+Math.random()*2;
this.k=-Math.PI+Math.random()*Math.PI;
this.rad=0;
//giving each snowflake unique characteristics
this._snowflake._xscale = this._snowflake._yscale=Math.random()*30;
this._snowflake._alpha = 75+Math.random()*100;
this._snowflake.onEnterFrame=function() {this.parent.snowflakeEnterFrame(this._snowflake);}
trace("SNOWFLAKE X:"+this._snowflake._x+" Y:"+this._snowflake._y);
trace(this._currentframe);
}
public function snowflakeEnterFrame() {
//putting it all together
this.rad += (k/180)*Math.PI;
this._snowflake._x -= Math.cos(rad);
this._snowflake._y += i;
if (this._snowflake._y>=Snowflake.MOVIE_HEIGHT) {
this._snowflake._y = -Snowflake.FALLING_SPEED;
}
if ((this._snowflake._x>=Snowflake.MOVIE_WIDTH) || (this._snowflake._x<=0)) {
this._snowflake._x = -Snowflake.WIND_SPEED+Math.random()*Snowflake.MOVIE_WIDTH;
this._snowflake._y = -Snowflake.WIND_SPEED;
}
this._snowflake._rotation+=Snowflake.ROTATION_SPEED;
}
public function vis(){
this._snowflake.visible = false;
}
}

Something like this (should work on any frame):
for(var i:Number = 0; i<snowflake.length; i++){
this.removeMovieClip(snowflake[i]._snowflake);
}
Loop through all the snowflake and remove them from the container.

Related

AS3 : accessing variable value

I have on stage 3 buttons every button should play a sound ... i'm trying to
make the class code dynamic .But i'm stuck with a problem please see the code .
i need help ! fla link & class link
i want to get the String inside the variable not only the name .
package
{
import flash.display.MovieClip;
import flash.events.MouseEvent;
public class CustumClass extends MovieClip
{
private var R1:String ="im a string inside R1";
private var R2:String ="im a string inside R2";
private var R3:String = "im a string inside R3";
private var btns:Array;
private var link:String;
public function CustumClass()
{
// constructor code
btns = new Array(r1,r2,r3); //___ buttons on stage
onLoop();
}
private function onLoop():void
{
for (var i:int = 0; i<btns.length; i++)
{
btns[i].addEventListener(MouseEvent.CLICK,handleBtn);
}
}
private function handleBtn(e:MouseEvent):void
{
// ____ i want to get the string inside variable
link = e.target.name.toUpperCase();
trace (link);
}
}
}
Instead of storing your buttons in an Array
private var btns:Array;
use a Dictionary
private var buttons:Dictionary = new Dictionary();
Then add each reference to a button along with the String that you want to associate with it
// constructor code
buttons[r1] = "text for r1";
buttons[r2] = "text for r2";
buttons[r3] = "text for r3";
onLoop();
You can iterate over the keys.
private function onLoop():void
{
for (var button:Object in buttons)
{
button.addEventListener(MouseEvent.CLICK,handleBtn);
// if above fails, try something like
IEventListener(button).addEventListener(MouseEvent.CLICK,handleBtn);
}
}
You can retrieve a value with a key
private function handleBtn(e:MouseEvent):void
{
trace (buttons[e.currentTarget]);
}

How to implement Data model using swift? (java -> swift)

in Java the model looks like this:
public class Model {
private String nameLast = "";
private String nameFirst = "";
private String namePrefix = "";
private String nameSecond = "";
private String nameNick = "";
private String nameSuffix = "";
Model() {
}
public Model(String nameFirst, String nameSecond, String nameLast, String namePrefix, String nameSuffix, String nameNick) {
this.nameFirst = nameFirst;
this.nameSecond = nameSecond;
this.nameLast = nameLast;
this.namePrefix = namePrefix;
this.nameSuffix = nameSuffix;
this.nameNick = nameNick;
}
public String getNameLast() {
return nameLast;
}
public void setNameLast(String nameLast) {
this.nameLast = nameLast;
}
public String getNameFirst() {
return nameFirst;
}
public void setNameFirst(String nameFirst) {
this.nameFirst = nameFirst;
}
public String getNamePrefix() {
return namePrefix;
}
public void setNamePrefix(String namePrefix) {
this.namePrefix = namePrefix;
}
....
}
In Swift there is no access control (private/public) and the setter/getters are only for computed properties.
How should I implement this in swift?
Should I use a struct?
Perhaps I have to use willSet and didSet - I need your help ;)
You can make it really simple in Swift. There are many similar parts in Swift.
class Model : NSObject{
var nameFirst = ""
var nameSecond = ""
var nameLast = ""
init(nameFirst:String, nameSecond:String, nameLast:String){
self.nameFirst = nameFirst
self.nameSecond = nameSecond
self.nameLast = nameLast
}
}
Then, instead of using a get or a set method, you can simply use the variable to set and get the values of the variable:
var mod = Model(nameFirst: "hey", nameSecond: "what's", nameLast: "up")
println(mod.nameLast)
While didSet and willSet are useful for counting, how many times a method was set, you don't need it in this case. Also in your case you dont need to set get for these variables like that:
var value : Int {
get { return 10 }
}

1067: Implicit Coercion of a value

So I'm currently trying to run my collision test on my two sprites, and I'm getting the following error:
C:\\Code\\Game\Game.as, Line 54, Column 34 1067: Implicit coercion of a value of type mvc:PlayerModel to an unrelated type assets.Scripts:SpriteAnimation.
Which is pointing to the following line of code:
handleSpriteToSpriteCollision(_player, _boss);
The function is as follows:
private function handleSpriteToSpriteCollision(sprite1:SpriteAnimation, sprite2:SpriteAnimation):void
{
var toSprite2 : VectorModel = new
VectorModel(0,0,0,0, sprite2.x - sprite1.x, sprite2.y - sprite1.y);
var bitmapData1:BitmapData = new BitmapData(sprite1.width, sprite1.height);
while(testBitmapCollision(sprite1.spriteFrameBitmapData, sprite1.topLeftX, sprite1.topLeftY, sprite2.spriteFrameBitmapData, sprite2.topLeftX, sprite2.topLeftY))
{
sprite2.x -= toSprite2.dx;
sprite2.y -= toSprite2.dy;
}
}
Both sprites display just fine, but as soon as I make the function call it all comes crumbling down. At this point I just need some fresh eyes to take a look at the code to see what's going wrong.
Edit: Here is the PlayerModel.as
package mvc
{
import flash.events.Event;
import flash.events.EventDispatcher;
import assets.Scripts.SpriteAnimation;
public class PlayerModel extends EventDispatcher
{
private var _previousX:Number = 0;
private var _previousY:Number = 0;
private var _xPos:Number = 0;
private var _yPos:Number = 0;
public var vx:Number = 0;
public var vy:Number = 0;
private var _height:uint = 30;
private var _width:uint;
private var _color:uint;
public function PlayerModel():void
{
}
public function update():void
{
xPos += vx;
yPos += vy;
}
public function get height():uint
{
return _height;
}
public function get color():uint
{
return _color;
}
public function get xPos():Number
{
return _xPos;
}
public function set xPos(value:Number):void
{
_xPos = value;
dispatchEvent(new Event(Event.CHANGE));
}
public function get yPos():Number
{
return _yPos;
}
public function set yPos(value:Number):void
{
_yPos = value;
dispatchEvent(new Event(Event.CHANGE));
}
public function set setX(value:Number):void
{
_previousX = value - vx;
xPos = value;
}
public function set setY(value:Number):void
{
_previousY = value - vy;
yPos = value;
}
}
}
The first thing i would check is the class of your boss and player objects inherit your SpriteAnimation class:
class PlayerModel extends SpriteAnimation
{ ...
Seems as though your handleSpriteToSpriteCollision function is looking for two SpriteAnimations and at least the PlayerModel being passed isn't one
Sprite inherits EventDispatcher so as long as your SpriteAnimation inherits Sprite as well, you shouldn't lose any functionality

ActionScript3: how to initialize array length in object after it has been declared?

I'm using an AS3 class that is:
package {
public class PeopleInfo {
public var elements:int;
public var PeopleName:Array;
public var PeopleInt:Array;
public var PeopleDecimal:Array;
}
}
In another file I've got:
<?xml version="1.0" encoding="utf-8"?>
<s:Application
creationComplete="initApp()"
xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx">
<fx:Script>
<![CDATA[
public var ii:int;
public var dataWriteToDB:PeopleInfo = new PeopleInfo;
public function initApp():void
{
// Initialize data provider array.
var int_data:Array = new Array(10, 20, 30, 40, 50);
var decimal_data:Array = new Array(13.11, 23.34, 35.69, 43.29, 58.92);
var name:Array = new Array("Joe", "Karen", "Simon", "Greg", "Alice");
dataWriteToDB.elements = 5; // number of elements in array
for (ii = 0; ii < dataWriteToDB.elements; ii++)
{
dataWriteToDB.PeopleName[ii] = name[ii];
dataWriteToDB.PeopleInt[ii] = int_data[ii];
dataWriteToDB.PeopleDecimal[ii] = decimal_data[ii];
}
}
and so on...
I'm getting a run-time error: Error #1009: Cannot access a property of method of a null object reference referring to the for loop's first line dataWriteToDB.PeopleName since it is NULL.
I'm guessing the problem here is that while dataWriteToDB is declared initially, the array lengths for the arrays in PeopleInfo class have not been set yet. Or, not sure otherwise why it's NULL. Anyone know how to clear this up?
The arrays have not been initialized in the class. You should do it in the declaration:
package {
public class PeopleInfo {
public var elements:int;
public var PeopleName:Array = [];
public var PeopleInt:Array = [];
public var PeopleDecimal:Array = [];
}
}
Also consider using push to add elements to the array, to avoid accidentally accessing a non-existing index:
for (ii = 0; ii < dataWriteToDB.elements; ii++)
{
dataWriteToDB.PeopleName.push(name[ii]);
dataWriteToDB.PeopleInt.push(int_data[ii]);
dataWriteToDB.PeopleDecimal.push(decimal_data[ii]);
}

Custom container not drawing

I've been reading about Vala over the past couple of days and decided to dive into it and make some Clutter widgets along the way. I'm currently trying to draw a private actor from my custom actor subclass. Here is a simplified version of what I've got so far.
public class MyContainer : Clutter.Actor, Clutter.Container {
private Clutter.Group group;
public MyContainer() {
group = new Clutter.Group();
group.set_parent(this);
}
public void add_actor(Clutter.Actor actor) {
group.add_actor(actor);
actor.show();
set_size(group.width, group.height);
actor_added(actor);
queue_redraw();
}
public void foreach(Clutter.Callback callback) {
group.foreach(callback);
queue_redraw();
}
public override void get_preferred_height(
float for_width,
out float min_height_p,
out float natural_height_p) {
group.get_preferred_height(
for_width,
out min_height_p,
out natural_height_p);
}
public override void get_preferred_width(
float for_height,
out float min_width_p,
out float natural_width_p) {
group.get_preferred_width(
for_height,
out min_width_p,
out natural_width_p);
}
public override void paint() {
group.paint();
}
public void remove_actor(Clutter.Actor actor) {
group.remove_actor(actor);
set_size(group.width, group.height);
actor_removed(actor);
queue_redraw();
}
public void sort_depth_order() {
group.sort_depth_order();
queue_redraw();
}
}
int main(string [] args) {
// Start clutter.
var result = Clutter.init(ref args);
if (result != Clutter.InitError.SUCCESS) {
stderr.printf("Error: %s\n", result.to_string());
return 1;
}
var stage = Clutter.Stage.get_default();
// Build a MyCollection object.
var myc = new MyContainer();
myc.x = 100;
myc.y = 100;
var r1 = new Clutter.Rectangle();
r1.width = 50;
r1.height = 50;
r1.color = Clutter.Color.from_string("rgb(255, 0, 0)");
var t1 = new Clutter.Text();
t1.text = "The red square.";
t1.y = r1.height;
// Build a Group object similar to the previous.
var group = new Clutter.Group();
group.x = 300;
group.y = 100;
var r2 = new Clutter.Rectangle();
r2.width = 50;
r2.height = 50;
r2.color = Clutter.Color.from_string("rgb(255, 0, 0)");
var t2 = new Clutter.Text();
t2.text = "The red square.";
t2.y = r2.height;
// Display.
myc.add_actor(r1);
myc.add_actor(t1);
group.add_actor(r2);
group.add_actor(t2);
stage.add_actor(myc);
stage.add_actor(group);
stage.show_all();
Clutter.main();
return 0;
}
The example paints the group added directly to the stage, but not the group wrapped by the custom collection that is added to the stage. How can I get this to work and what is wrong with the above?
I've been working on ubuntu 11.10 with valac --pkg clutter-1.0 above_code_example.vala.
This answer is from buz on gnome.irc's #clutter room.
The problem is a missing override for the allocate function.

Resources