I have an array learnnum that looks like [0,1,1,0,1,1,1,1,0].
I need to basically ask the user for an input Left Mouse Button or Right Mouse Button. If Left, then the values of learnnum of [i] is flipped, else nothing happens. I only do this for i=1,3,5,7. I have written the below code, but it does not work properly, instead of going for all the 4 conditions... it directly goes to 4. It seems that it is not waiting for the input conditions... Is there any way I can correct this?
function changeNumba(i)
{ //check1=true;
print ("PRINTT "+check1);
while(!Input.GetButtonDown("Fire1") && !Input.GetButtonDown("Fire2"))
{
if(Input.GetButtonDown("Fire1"))
{
check1++;
}
if(Input.GetButtonDown("Fire2"))
{
learnednum[i]=0 ? 1 : 0;
check1++;
}
}
}
function changelearn()
{
//FIRST STEP
//if(check1)
if(move1==9 && check1==0)
{changeNumba(1);
}
//SECOND STEP
if(move1==9 && check1==1)
{changeNumba(3);
}
if(move1==9 && check1==2)
{changeNumba(5);
}
if(move1==9 && check1==3)
{changeNumba(7);
}
}
var check1=0;
//1,3,5,7
function Update () {
if(move1==9)//this is just a game condition. Do not bother about it.
{
changelearn();
}
}
from looking at the unity script api:
http://docs.unity3d.com/Documentation/ScriptReference/Input.GetButtonDown.html
you should not have a while() loop inside of your Update() method.
change changeNumba() as follows:
function changeNumba(i)
{
if(Input.GetButtonDown("Fire1")){
check1++;
}
if(Input.GetButtonDown("Fire2")){
learnednum[i] = learnednum[i]==0 ? 1 : 0;
check1++;
}
}
Related
I understand that we can initiate keypress events like Backspace or ENTER in protractor with the help of protractor.Key.BACK_SPACE or ENTER. But , how do i do this action multiple times? Is it just adding another line of it or any good approach towards it?
We have written a common functions to do the same.
exports.commonfunc = {
pressKey: function(key) {
switch (key) {
case 'Enter':
browser.actions().sendKeys(protractor.Key.ENTER).perform();
break;
case 'Backspace':
browser.actions().sendKeys(protractor.Key.BACK_SPACE).perform();
break;
}
},
pressKeyNtimes: function(key, n) {
for (i = 1; i <= n; i++) {
this.pressKey(key);
}
}
}
Now we using commonfunc in another class as below :
exports.Login = {
commonfunc: Object.create(require('../common/commonfunctions.js').commonfunc),
iClickEnterTwice: function(){
this.commonfunc.pressKeyNtimes('Enter',2);
}
}
Hope this helps!
I have a checkbox that adds +2 if the user checks it. I need it to only do this one time. Right now the user can constantly add plus 2 which is not what I need. I need it to add it only once. (factors.Delivery is the ng-model)
I also need it to remove the --2 and stop if the checkmark is removed, but right now, it keeps subtracting.
Any help would be appreciated
Even disabling the check after clicking would be good, but it wont disable after clicking only before.
$scope.factors.Delivery = 0;
$scope.increment = function() {
if ($scope.factors.Delivery >= 0) {
$scope.factors.Delivery+=2;
} else {
$scope.factors.Delivery-=2;
}
};
2 is greater than or equal to 0. It sounds like the only acceptable values are 2 and 0 based on your post. I would suggest:
$scope.factors.Delivery = 0;
$scope.increment = function() {
if ($scope.factors.Delivery == 0) {
$scope.factors.Delivery=2;
} else {
$scope.factors.Delivery=0;
}
};`
or using your code, you can disable the checkbox with something like this:
ng-disabled="clickedOnce" on the checkbox and the modifications below.
$scope.factors.Delivery = 0;
$scope.clickedOnce = false;
$scope.increment = function() {
$scope.clickedOnce = true;
if ($scope.factors.Delivery >= 0) {
$scope.factors.Delivery+=2;
} else {
$scope.factors.Delivery-=2;
}
};
I have the following:
$scope.$watch('duration.dayPreference',function(value){
console.log(value);
if(value=='every')
{
that.duration.days = 1;
}
else if(value=='selected')
{
//alert('test');
that.duration.days=[];
}
else if(value=='everyday')
{
that.duration.days='everyday';
}
});
this.selectDay = function (day) {
$scope.duration.dayPreference = 'selected';
//$scope.$apply();
/*if(typeof(this.duration.days)!='object')
{
this.duration.days=[];
}*/
var index = this.duration.days.indexOf(day);
if (index == -1) {
//alert('test2');
this.duration.days.push(day);
}
else {
this.duration.days.splice(index, 1);
}
}
In this, when I do $scope.duration.dayPreference = 'selected'; I expect the line below it to have the this.duration.days set to a blank array. But it doesn't. Upon a closer inspection, I found that the callback in the $watch runs after the line below the assignment.
It may be very probable that, $watch may be using some kinda timers internally. What should be the way to do it then.
The watch won't be triggered until the digest is run. This will be after your entire function is compete.
If you consider that AngularJS is itself written in JavaScript, there would be no way for it to react to your setting of a property at the time. You are using the thread yourself. It can only wait for you to finish and then react.
As for what to do instead...
Perhaps you could call that watch function manually?
Or maybe the code which expects the empty array should belong inside the watch?
Watch will trigger on the $digest, which will occur after current cycle/code finishes running. You need to figure out a way of rearranging your code that handles things asynchronously. One possible quick solution might be:
var selectedDays = [];
$scope.$watch('duration.dayPreference',function(value){
console.log(value);
if(value=='every')
{
that.duration.days = 1;
}
else if(value=='selected')
{
//alert('test');
that.duration.days = selectedDays;
}
else if(value=='everyday')
{
that.duration.days='everyday';
}
});
this.selectDay = function (day) {
$scope.duration.dayPreference = 'selected';
var index = selectedDays.indexOf(day);
if (index == -1) {
//alert('test2');
selectedDays.push(day);
}
else {
selectedDays.splice(index, 1);
}
}
I got the result I wanted - but I am doing it in a way that is just, wow.
I keep finding the right array within an array, and then doing a foreach and if to find the next correct array. Is there a more sanitary way to do this? My code is below:
angular.forEach($scope.bitrates, function(bitrateset) {
if(bitrateset.resolution == cam.camera.resolution){
angular.forEach(bitrateset.stats, function(stat) {
if(stat.activity == cam.activity){
angular.forEach(stat.fps, function(fps) {
angular.forEach(fps.range, function(range) {
if(range == cam.fps){
cam.bitrate = fps.bitrate;
}
else{
}
});
});
}
});
}
});
I took over a project and I'm really new to as3! I need to fix an unresolved problem hopefully without starting over from scratch.
I have a contaner_mc, holding four puppet_mc's (puppet_mc1, puppet_mc2, puppet_mc3, puppet_mc4) and each puppet_mc holds four buttons (buddy_bnt_1, buddy_bnt_2, buddy_bnt_3, buddy_bnt_4).
I need the listener to know what button was clicked and from what puppet that button was clicked?
Object(this).contaner_mc.puppet_mc?.buddy_bnt_?.addEventListener(MouseEvent.CLICK, fl_Click);
function fl_Click(event:MouseEvent):void
{
gotoAndPlay(15);
}
Any advice would be greatly appreciated!
Event bubbling should to the magic:
container_mc.addEventListener(MouseEvent.CLICK, click_handler);
function click_handler(event:MouseEvent):void {
if (event.currentTarget == event.target) {
return; // the container is the dispatcher
}
const sprite:Sprite = event.target as Sprite; // just for a typing
if (!sprite) {
trace("event.target is no sprite, set break point here for debugging.");
return;
}
if (sprite.name == 'buddy_bnt_1' || sprite.name == 'puppet_mc1') {
// do something
} else if (sprite.name == 'buddy_bnt_2' || sprite.name == 'puppet_mc2') {
// do something different
} else if (sprite.name == 'buddy_bnt_3' || sprite.name == 'puppet_mc3') {
// do something different
} else if (sprite.name == 'buddy_bnt_4' || sprite.name == 'puppet_mc4') {
// do something different
}
}
container_mc.addEventListener(MouseEvent.CLICK, clickHandler);
function clickHandler(event:MouseEvent):void
{
var clickedObject:DisplayObject = event.target as DisplayObject;
trace (clickedObject.name); // name of button clicked
trace (clickedObject.parent.name); // name of the parent
}
I used DisplayObject so that the code would work with a sprite , movieclip, or any DisplayObject. So make sure you import if you haven't already :
import flash.display.DisplayObject;