I'm trying to access instance variables from within a nested object ('action'). The only workaround I could find was using a local var ('_') to represent the parent object.
Class Mover
...
Constructor () =>
_ = this
#mode = "wave"
#action= {
wave: ->
_.x = _.ox
_.y = _.oy = Math.cos(window.G.time * _.speed + _.c + _.vary) * _.amp - _.amp * .5
return
jump: ->
_.y = _.oy = Math.min(0,Math.cos(window.G.time*_.speed + _.c * _.vary)) * _.amp - _.amp * .5
_.x = _.ox
return
#loop =>
#action[#mode]()
Try using a fat arrow (=>) inside your action object like so:
wave: =>
#x = #ox
#y = #oy = Math.cos(window.G.time * #speed + #c + #vary) * #amp - #amp * .5
return
Related
i am working on tunable parameters and with a manual s-function its pretty straight forward. but what to do when we want to make variables tunable (getting them as parameters from s-function parameter block ) and generating the s-function automatically using the legacy code tool.
is it even possible?
for example i have written a simple single track model. This is the single_track.h file
#ifndef _SINGLE_TRACK_FUNC_
#define _SINGLE_TRACK_FUNC_
typedef struct retvale
{
double a_y;
double psi_dot;
double beta;
} retvale;
extern struct retvale singletrack(double a,double b,double m,double cv,double ch,double lv,double lh,double theta,double I_s, double dt);
extern void initialization();
#endif
this is the single_track.c file
#include "single_track_func.h"
float a_1_1,a_1_2,a_2_1,a_2_2,b_1_1,b_2_1,psi_dot_prev,beta_prev;
int count;
retvale singletrack(double a,double b,double m,double cv,double ch,double lv,double lh,double theta,double I_s, double dt)
{
retvale my_obj;
static float beta_dot=0;
static float psi_double_dot=0;
static float beta_previous=0;
static float psi_dot_previous=0;
beta_previous = beta_prev;
psi_dot_previous = psi_dot_prev;
a_1_1 = ((-cv-ch)/((m)*(a)));
a_1_2 = ((m*(a)*(a))-((ch*lh)-(cv*lv)))/(m*(a)*(a));
a_2_1 = (-(ch*lh)+(cv*lv))/theta;
a_2_2 = ((-ch*lh*lh)-(cv*lv*lv))/(theta*(a));
b_1_1 = -cv/(m*(a));
b_2_1 = (cv*lv)/(theta);
beta_dot = a_1_1 * beta_previous + a_1_2 * psi_dot_previous - b_1_1*((b)/I_s);
psi_double_dot = a_2_1 * beta_previous + a_2_2 * psi_dot_previous + b_2_1*((b)/I_s);
my_obj.beta = beta_dot * dt + beta_previous;
my_obj.psi_dot = psi_double_dot * dt + psi_dot_previous;
my_obj.a_y = (a*((my_obj.psi_dot)-(beta_dot)));
beta_prev=my_obj.beta;
psi_dot_prev=my_obj.psi_dot;
return my_obj;
}
void initialization()
{
psi_dot_prev=0;
beta_prev=0;
}
the arguments from double m till double dt are the inputs which i expect as parameters from the s-function block while the 'double a' and 'double b' values i will get from inputs so i don't have to worry about them.
manually i introduced some global variables in my s-function in mdlInitializeSize
ssSetNumSFcnParams(S, 8);
m_s=mxGetPr(ssGetSFcnParam(S,0));
cv_s=(real_T*) mxGetPr(ssGetSFcnParam(S,1));
ch_s=(real_T*) mxGetPr(ssGetSFcnParam(S,2));
lv_s=(real_T*)mxGetPr(ssGetSFcnParam(S,3));
lh_s=(real_T*)mxGetPr(ssGetSFcnParam(S,4));
theta_s=(real_T*)mxGetPr(ssGetSFcnParam(S,5));
I_s_s=(real_T*)mxGetPr(ssGetSFcnParam(S,6));
dt_s=(real_T*)mxGetPr(ssGetSFcnParam(S,7));
ssSetSFcnParamTunable(S,0,SS_PRM_SIM_ONLY_TUNABLE);
ssSetSFcnParamTunable(S,1,SS_PRM_SIM_ONLY_TUNABLE);
ssSetSFcnParamTunable(S,2,SS_PRM_SIM_ONLY_TUNABLE);
ssSetSFcnParamTunable(S,3,SS_PRM_SIM_ONLY_TUNABLE);
ssSetSFcnParamTunable(S,4,SS_PRM_SIM_ONLY_TUNABLE);
ssSetSFcnParamTunable(S,5,SS_PRM_SIM_ONLY_TUNABLE);
ssSetSFcnParamTunable(S,6,SS_PRM_SIM_ONLY_TUNABLE);
ssSetSFcnParamTunable(S,7,SS_PRM_SIM_ONLY_TUNABLE);
this works manually but how to put it in legacy code such that it works automatically
i have written legacy code previously where I was using predefined values and it worked fine:
run('CreateBusObject_retvale');
def = legacy_code('initialize');
def.SourceFiles = {'single_track_func.c'};
def.HeaderFiles = {'single_track_func.h'};
def.SFunctionName = 'single_track_1';
def.StartFcnSpec ='void initialization()';
def.OutputFcnSpec ='retvale y1=singletrack(double u1,double u2, double u3,)';
def.SampleTime = [-1,0];
legacy_code('sfcn_cmex_generate', def);
legacy_code('compile', def);
legacy_code('sfcn_tlc_generate', def);
and CreateBusObject_retvale.m is
a_y=Simulink.BusElement;
a_y.Name='a_y';
a_y.DataType='double';
psi_dot=Simulink.BusElement;
psi_dot.Name='psi_dot';
psi_dot.DataType='double';
beta=Simulink.BusElement;
beta.Name='beta';
beta.DataType='double';
retvale = Simulink.Bus;
retvale.HeaderFile = 'single_track_func.h';
retvale.Elements = [a_y,psi_dot,beta];
outputBus = Simulink.BusElement;
outputBus.Name = 'outputBus';
outputBus.DataType = 'Bus: retvale';
I am fairly new to java and am currently debugging a program. So I have a class that has a simple method with a few calculations. Basically when I call this method from another class to use a variable (that happens to be part of an array where some of it is being calculated) I get the calculation I need. Then I call it once more in another class, it calculates again and my numbers become compromised. Is there a way to have this method take in some value, calculate and then stay put?
This is the array class. I need to instantiate this once and then have the values for the arrays be static when I call it again. Is there a way to make the method stay put while I just call the arrays?
In another class I use PlanMat pm = new PlanMat()
then something like pm.materials(fSi, fS, fO); and to call an array pm.rho0[48];
public class PlanMat {
Constants con = new Constants();
double GPa = con.GPascals;
Main m = new Main();
public int i ;
public int eos [ ];
public double rho0 [ ];
public double c [ ];
public double nn [ ];
public double Ks0 [ ];
public double Ksp [ ];
public void materials(double fSi, double fS, double fO)
{
i = 0;
eos = new int [ 51 ];
rho0 = new double [ 51 ];
c = new double [ 51 ];
nn = new double [ 51 ];
Ks0 = new double [ 51 ];
Ksp =new double [ 51 ];
double fFeS = ( ((con.M_Fe / con.M_S) + 1) * fS );
double fFeSi = ( ((con.M_Fe / con.M_Si) + 1) * fSi);
double fFeO = ( ((0.950 * con.M_Fe) + con.M_O) / (1.95 * con.M_O) * fO);
double fFe = 1.0 - fFeS - fFeSi - fFeO;
i = 48;
eos [ i ] = 1;
rho0 [ i ] = 1.0 / ( (fFe / rho0[3]) + (fFeSi / rho0[21])
+ (fFeO / rho0[22]) + (fFeS / rho0[13]) );
Ks0 [ i ] = fFe * Ks0[3] + fFeSi * Ks0[21] + fFeO * Ks0[22]
+ fFeS * Ks0[13];
Ksp [ i ] = fFe * Ks0[3] + fFeSi * Ks0[21] + fFeO * Ks0[22]
+ fFeS * Ks0[13];
c [ i ] = 0.0;
nn [ i ]= 0.0;
}
}
There are two wasys to acheive this:
First:
You should write set and get methods, call to set method to set the values and get method to get the values. You would be setting the values say in class1 you set the values and now if you want the same values in class2 to you would be calling your get method to get the values as an array and pass this array as an argument to the constructor of class2.
Second:
You can pass the whole object to the class2 constructor and use that object in that class your values will be same and data will not be compromised but for this you will have to implement the Serializable on your data class(That you are using in both.)
class a{
......
function()
{
a,b
}
}
class b{
...function(){
here u need your variable value.
call the function and store the variable value in new variable.
}
}
if u need to do same u can do
class c{
...function(){
here u need your variable value.
call the function and store the variable value in new variable.
}
}
I'm new with corona/lua and i'm i can't find a solution to this thing. I'm trying to spawn a object that fall from top to down and should stop at the bottom of the screen. Then i'll create the touch event etc etc..
but for now the problem is that i recieve this error:
attempt to index global 'physics' (a nil value)
and objects ofc doesn't fall down.
here is my code:
-----------------------------------------------------------------------------------------
--
-- main.lua
--
-----------------------------------------------------------------------------------------
local buttonY = display.contentWidth * 0.02
local buttonWidth = display.contentWidth * 0.1
local buttonHeight = display.contentWidth * 0.1
background = display.newImage("graphics/background.jpg")
local localGroup = display.newGroup()
local spawnTable = {}
function spawnLattina(params)
local object = display.newImage(params.image, params.buttonX,50);
object.objTable = params.objTable;
object.index = #object.objTable+1;
object.name = "object:".. object.index;
--fisica
if params.hasBody then
object.density = params.density or 0;
object.friction = params.friction or 0;
object.bounce = params.bounce or 0;
object.isSensor = params.isSensor or false;
object.bodyType = params.bodyType or "dynamic";
print(object.density .. " Friction: ".. object.friction .."bodyType: "..object.bodyType)
physics.addBody(object, object.bodyType,
{density = object.density,
friction = object.friction,
bounce = object.bounce}
)
end
object.group = params.group or nil
object.group:insert(object)
object.objTable[object.index] = object
return object
end
for i = 1, 2 do
local spawns = spawnLattina(
{
image = "graphics/lattina.png",
objTable = spawnTable,
buttonX = math.random(50,480),
hasBody = true,
density = 0,
friction = 12,
bodyType = "static",
group = localGroup,
}
)
end
You haven't started the physics engine. Write the following lines on the top of your class:
local physics = require "physics"
physics.start()
Keep Coding.................. :)
I'm struggling to get any of the mouse events working with the latest easeljs library (easeljs-0.6.1.min.js).
I'm using TypeScript (the definition of which seems to be up to date).
My stage, container etc are set up as follows:
stage = new createjs.Stage("gameCanvas");
container = new createjs.Container();
stage.addChild(container);
createjs.Touch.enable(stage);
I then have my entity related code which looks like this:
Egg.prototype.wireUpEggForMovement = function () {
Game.get().container.addChild(this.bitmap);
this.bitmap.mousedown = function (evt) {
var o = evt.target;
Game.get().container.addChild(this.bitmap);
var offset = { x: o.x - evt.stageX, y: o.y - evt.stageY };
this.gamePosition = new Point(offset.x, offset.y);
evt.onMouseMove = function (ev) {
o.x = ev.stageX + offset.x;
o.y = ev.stageY + offset.y;
};
};
this.bitmap.mouseover = function (evt) {
var o = evt.target;
o.scaleX = o.scaleY = 1.2;
};
this.bitmap.mouseout = function (evt) {
var o = evt.target;
o.scaleX = o.scaleY = 1;
};
};
this.bitmap, stage and container all exist.
None of the mouse events are triggered however.
Any ideas?
If you are using the mousedown-keyword you have to use it like this:
this.bitmap.addEventListener('mousedown', function...)
http://www.createjs.com/Docs/EaselJS/classes/Bitmap.html#method_addEventListener
OR: If you want to set a listener-function via an attribute(which is deprecated), you would have to use .onPress = function... to get the mouse-down event. But since this is deprecated I suggest you use the addEventListener()
Okay so I know how to move the stage within the actual .fla file by modifying this.x and this.y variables in the layer 1 actionscript.
But within the document class- public class Starlight extends MovieClip, it does not seem to work no matter what i try and my research lead me to this use code instead:
for( i = 0; i < stage.numChildren; i ++){
stage.getChildAt(i).x -= player.speedx * player.bounceSpeed;
stage.getChildAt(i).y -= player.speedy * player.bounceSpeed;
}
I do realize that its hacky and slower as compared to actually moving the stage itself. And i'm not sure what's going to happen if another object that moves comes into the stage because technically this code is unnaturally altering the x,y of everything in the stage.
Any help appreciated!!
Cheers
Edit: Tried this--
var stage2:Sprite = new Sprite();
stage2.x = stage.stageWidth / 2;
stage2.y = stage.stageHeight / 2;
stage2.width = 4000;
stage2.height = 4000;
addChild(stage2);
for (i = 1; i < 50; i ++)
{
var asteroid:Asteroid = new Asteroid();
asteroid.x = Math.round(Math.random() * stage.stageWidth * 4);
asteroid.y = Math.round(Math.random() * stage.stageHeight * 4);
stage2.addChild(asteroid);
collisionList.addItem(asteroid);
asteroids.push(asteroid);
}
Woah, don't move the stage!
Create a MovieClip or a Sprite and stick everything in there, then it's just a case of moving that object.
var stage2:Sprite = new Sprite();
stage2.addChild(something);
stage2.addChild(somethingElse);
stage2.x = 10;
stage2.y = 10;